放置代码表示“尝试使用 'GetMouse' 索引 nil”,但在需要它的代码之后我没有得到任何可变代码

A placement code is saying that 'attempt to index nil with 'GetMouse'' and I have not got any variable codes after a code that needs it

我已经尝试过我的其他问题之一的答案,但没有成功。我是 Lua 的新手,所以我可能遗漏了什么。

local player = game.Players.LocalPlayer
local Mouse = player.GetMouse()
local Block = game.ServerStorage.Experimental

Mouse.Button1Down(place)

function place()
    Mouse.Hit.X = PosX
    Mouse.Hit.Y = PosY
    Mouse.Hit.Z = PosZ

    PlacedBlock = Block:Clone()
    PlacedBlock.Parent = game.Workspace
    PlacedBlock.Position = Vector3.new(PosX,PosY,PosZ)
end

这里的简单错误是您在调用 GetMouse() 时使用了 . 而不是 :。替换后,您的代码应该可以正常工作:D

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local Block = game.ServerStorage.Experimental

function place()
    PlacedBlock = Block:Clone()
    PlacedBlock.Parent = workspace
    PlacedBlock.Position = Mouse.Hit.p
end

Mouse.MouseButton1Click:Connect(place)

这应该有效!