为什么使用Roblox Lua Players.LocalPlayer.Mouse:Getmouse()时出现错误?

我试图制作游戏,但它严重依赖鼠标,但出现错误,说'attempt to index nil with 'GetMouse'',无论我做什么都会创建此错误。也许我的游戏没有更新(我认为它已经更新了)。我以前问过类似的问题,但现在我确定是Roblox Studio的问题。

下面是代码:


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)
点赞
用户13116304
用户13116304

第一个问题是 MouseButton1Click 不是 GetMouse() 的有效成员。MouseButton1Click 用于诸如 GUI 对象之类的内容。Button1Down 用于 GetMouse()。另外,.p 已不再使用,请使用 .Position 替代。

其次,你需要将你的部件放在 ReplicatedStorage 中,而不是 ServerStorage 中。客户端无法访问 ServerStorage。确保你正在使用 LocalScript

修正后:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Block = game.ReplicatedStorage.Experimental

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

mouse.Button1Down:Connect(place)

现在有一个问题,当你放置部件时,只有你自己能看到,其他人看不到。为了解决这个问题,你需要使用 _Remote Events_。

2020-05-10 16:33:19