Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to make this give the tool to the Player who triggered the script?

Asked by 5 years ago

Basically, I'm messing around with my new GUI I made. When clicked it Fires a RemoteEvent called 'give_event' when triggered, It gives the tools to me. But the problem is, I want it to be given to the person who clicked... I don't want to modify the ServerScript's name Here is what I got:

game.ReplicatedStorage.give_event.OnServerEvent:Connect(function()
    game.ServerStorage:FindFirstChild("F3X")
    game.ServerStorage.F3X.Parent = game.Workspace
    wait(0.1)
    game.Workspace.F3X.Parent = game.Players.PlayerName.StarterGear
end)

(Yes that's the ServerScript triggered by the RemoteEvent.)

Any help would be appreciated, thanks in advance.

0
Btw, for line 5 I meant Backpack. ExtremeRisk 9 — 5y
0
Any errors? Rheines 661 — 5y

1 answer

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

OnServerEvent has a default player argument, and we can use this to modify the player's backpack. Also, you need to clone the tool and parent it, as only parenting it to somewhere will only move the tool to the player's backpack and will only work once.

FindFirstChild returns nilif the tool is not found in the ServerStorage, and this may happen when the script runs first before the tool has loaded. If you are really sure that the tool exists in ServerStorage, use WaitForChild instead, and the script will yield until the tool has loaded.

--Get the player who fired the RemoteEvent using the default player argument.
game.ReplicatedStorage.give_event.OnServerEvent:Connect(function(player)
    --Clone the tool from server storage after waiting for it to load.
    local tool = game.ServerStorage:WaitForChild("F3X"):Clone()
    --Parent it to the player's backpack.
    tool.Parent = player.Backpack
end)
Ad

Answer this question