英文:
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
但是当您调用RemoteEvent的FireServer函数时,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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论