Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

2 comments

0

Btw, for line 5 I meant Backpack.

0

Any errors?

1 answer

✓ Accepted answer

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 nil if 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)

This discussion is preserved as it was. New answers, comments, and votes are closed.