在 roblox 中使用远程事件将人们从监狱中救出似乎无法正常工作
Bailing people out of prison in roblox using Remote Events doesn't seem to work properly
这是交易。当你在我的游戏中被捕时,你就会被送进监狱。要想出去,你必须得到保释。
客户端向服务器发送保释请求。我相信除了这一部分之外,其他所有部分似乎都有效,但它可能是客户端脚本。这个脚本有什么不正确的地方吗?我已经检查过它是否有任何对我来说很明显的错误。
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 .. ' has Bailed ' .. PlayerToBail.Name), Color3.fromRGB(0, 250, 0))
end
end
end)
有效的本地脚本:
script.Parent.AcceptButton.MouseButton1Click:Connect(function()
local PlayerName = script.Parent.TargetName.Text
game.ReplicatedStorage.Bail:FireServer(PlayerName)
print ("Bail Requested")
end)
我认为问题出在您传递给远程事件的参数上。
在您的客户端脚本中,您将 PlayerName 作为参数传递。我假设这是玩家姓名的字符串:
game.ReplicatedStorage.Bail:FireServer(PlayerName)
PlayerName 实际上会被发送到参数“PlayerToBail”,我假设它应该是一个玩家对象。请记住,Roblox 的 RemoteEvents 会自动将触发远程事件的玩家作为第一个参数传入。因此,连接到远程事件的函数的“播放器”参数是具有触发远程事件的本地脚本的实际播放器对象。
相反,我会像这样触发远程事件:
game.ReplicatedStorage.Bail:FireServer()
由于您要保释的玩家会自动作为参数传递,因此您无需向 FireServer 添加任何其他参数。此外,您还可以删除服务器脚本中的“PlayerToBail”参数。
另请注意,您的服务器脚本中无需包含这行代码:
Player = game.Players:FindFirstChild(Player)
玩家已经在引用 game.Players 中的对象。另外,Player是一个对象,不是字符串。所以这是行不通的。您可以简单地按原样使用 Player。
有关远程事件的更多信息:https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
如有问题欢迎随时跟进
当触发服务器时,它会自动给出一个参数,即发送它的玩家。因此,您不必拥有 playerName 部分,因为在 player 参数已经存在之后,您可以从中获取名称。
这是交易。当你在我的游戏中被捕时,你就会被送进监狱。要想出去,你必须得到保释。
客户端向服务器发送保释请求。我相信除了这一部分之外,其他所有部分似乎都有效,但它可能是客户端脚本。这个脚本有什么不正确的地方吗?我已经检查过它是否有任何对我来说很明显的错误。
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 .. ' has Bailed ' .. PlayerToBail.Name), Color3.fromRGB(0, 250, 0))
end
end
end)
有效的本地脚本:
script.Parent.AcceptButton.MouseButton1Click:Connect(function()
local PlayerName = script.Parent.TargetName.Text
game.ReplicatedStorage.Bail:FireServer(PlayerName)
print ("Bail Requested")
end)
我认为问题出在您传递给远程事件的参数上。 在您的客户端脚本中,您将 PlayerName 作为参数传递。我假设这是玩家姓名的字符串:
game.ReplicatedStorage.Bail:FireServer(PlayerName)
PlayerName 实际上会被发送到参数“PlayerToBail”,我假设它应该是一个玩家对象。请记住,Roblox 的 RemoteEvents 会自动将触发远程事件的玩家作为第一个参数传入。因此,连接到远程事件的函数的“播放器”参数是具有触发远程事件的本地脚本的实际播放器对象。
相反,我会像这样触发远程事件:
game.ReplicatedStorage.Bail:FireServer()
由于您要保释的玩家会自动作为参数传递,因此您无需向 FireServer 添加任何其他参数。此外,您还可以删除服务器脚本中的“PlayerToBail”参数。
另请注意,您的服务器脚本中无需包含这行代码:
Player = game.Players:FindFirstChild(Player)
玩家已经在引用 game.Players 中的对象。另外,Player是一个对象,不是字符串。所以这是行不通的。您可以简单地按原样使用 Player。
有关远程事件的更多信息:https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
如有问题欢迎随时跟进
当触发服务器时,它会自动给出一个参数,即发送它的玩家。因此,您不必拥有 playerName 部分,因为在 player 参数已经存在之后,您可以从中获取名称。