如何使用远程事件为人们提供工具?

huangapple go评论63阅读模式
英文:

How to give people tools using a remote event?

问题

我有一个本地脚本,当点击某个部分时,它会将玩家的名称发送到服务器端,以便分发武器。事件触发时,会进入服务器端脚本中的此代码:

ReplicatedStorage.RemoteEvent:OnServerEvent:Connect(function(playerWhoWantsTool)
     local toolClone = tool:Clone()
     print(playerWhoWantsTool)-- 它会在输出中打印出我的用户名,所以我知道它能工作
     local buyersBackPack = game.Players.playerWhowantsTool.Backpack
     toolClone.Parent = buyersBackPack
     -- 返回一个关于在game.Players中找不到playerwhowantsTool的错误
end)

我不确定如何在服务器脚本中为玩家提供一个工具,谢谢。

我期望识别的玩家能在他们的背包中收到这个工具。

英文:

I have a local script that says when a part is clicked it will send the name of the player to the serverside for handing out the weapon. When the the event is fired it goes to this code in the server script side

ReplicatedStorage.RemoteEvent:OnServerEvent:Connect(function(playerWhoWantsTool)
     local toolClone = tool:Clone()
     print(playerWhoWantsTool)-- It prints out my username in the output so I know it works
     local buyersBackPack = game.Players.playerWhowantsTool.Backpack
     toolClone.Parent = buyersBackPack
     -- Returns an error about cant find playerwhowantsTool in game.Players
end)

I'm not sure how to go about giving a player a tool with a server script,Thanks.

I was expecting for the identified player to receive the tool in their backpack.

答案1

得分: 0

但是当您调用RemoteEventFireServer函数时,OnServerEvent事件将提供触发事件的玩家作为第一个参数。该变量是一个Player对象,因此您可以直接从该对象访问背包。您不需要再索引Players服务以找到玩家。

ReplicatedStorage.RemoteEvent:OnServerEvent:Connect(function(player)
     local toolClone = tool:Clone()
     toolClone.Parent = player.Backpack
end)
英文:

But when you call a RemoteEvent's FireServer function, the OnServerEvent event will provide the player that fired the event as the first argument. That variable is a Player object, so you have access to the Backpack right from that object. You don't need to index into the Players service to find the player again.

ReplicatedStorage.RemoteEvent:OnServerEvent:Connect(function(player)
     local toolClone = tool:Clone()
     toolClone.Parent = player.Backpack
end)

huangapple
  • 本文由 发表于 2023年6月5日 20:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定