在 roblox 中使用 Remote Events 把人从监狱中解救出来似乎无法正常工作

这是事实。当你在我的游戏中被逮捕时,你会被送进监狱。要出去,你必须被保释。

客户端发送请求到服务器来保释他们。除了这一部分,其他所有部分似乎都工作正常,但我认为这可能是客户端脚本的问题。这个脚本有什么问题吗?我已经检查过其中任何我能发现的错误。

local replicatedStorage = game:GetService('ReplicatedStorage')
local createSystemMessage = replicatedStorage:WaitForChild('CreateSystemMessage')

game.ReplicatedStorage.Bail.OnServerEvent:Connect(function(Player,PlayerToBail)
        Player = game.Players:FindFirstChild(Player)

        local tab = nil
    for i,v in pairs(_G.GlobalData) do
        if v.Name == Player.Name then
            tab = v
        end
    end
    if PlayerToBail.Team == game.Teams:FindFirstChild("Criminal") then
        local Bounty = PlayerToBail.leaderstats.Bounty.Value * 2
     if tab.Bank <= Bounty then
        tab.Bank -= Bounty
            PlayerToBail.leaderstats.Bounty.Value = 0
            PlayerToBail.Prisoner.Value = false
            PlayerToBail.Team = game.Teams:FindFirstChild("Civilian")
            createSystemMessage:FireAllClients((Player.Name .. ' 保释了 ' .. PlayerToBail.Name), Color3.fromRGB(0, 250, 0))

end
end

end)

还有工作的本地脚本:

script.Parent.AcceptButton.MouseButton1Click:Connect(function()
            local 玩家姓名 = script.Parent.TargetName.Text
            game.ReplicatedStorage.Bail:FireServer(玩家姓名)
            print ("请求保释")
end)
点赞
用户13788973
用户13788973

我认为问题出在你传递给远程事件的参数上。

在客户端脚本中,你将PlayerName作为参数传递。我假设这是玩家名字的字符串:

game.ReplicatedStorage.Bail:FireServer(PlayerName)

实际上,PlayerName将被发送到参数“PlayerToBail”中,我假设这应该是玩家对象。请记住,Roblox的RemoteEvents会自动将触发远程事件的玩家作为第一个参数传递。所以与你的远程事件连接的函数的"Player"参数是实际拥有触发远程事件的本地脚本的玩家对象。

相反,我会用这种方式触发远程事件:

game.ReplicatedStorage.Bail:FireServer()

由于你想要救援的玩家会自动作为一个参数传递,所以你不需要在FireServer中添加任何其他参数。此外,你需要在服务器脚本中删除“PlayerToBail”参数。

还要注意,你不需要在服务器脚本中加入这一行代码:

Player = game.Players:FindFirstChild(Player)

Player已经在game.Players中引用了一个对象。此外,Player是一个对象,而不是一个字符串。所以这行代码不会起任何作用。你可以直接使用Player来达到你的目的。

有关Remote Events的更多信息:https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

如果你仍然有问题,请随时跟进。

2020-07-01 17:46:00
用户15710593
用户15710593

当启动服务器时,它会自动提供一个参数,即发送该命令的玩家。因此,在使用该参数之后,您不必再使用playerName部分,因为已经包含了玩家参数并且您可以从中获取玩家名称。

2021-04-20 21:32:48