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

How do I parent a script only to a specific player who joins?

Hello!

So currently I have a script, it doesnt effect this question, but whenever a specific player joins, either if its their roblox name, or ID, how could I make the script parent itself to that player whenever they join?

Im not sure if I use "WaitForChild", because until that player joins, the rest of the script will not play. If you could help me, then I would appreciate it.

1 comment

0

y not put the script to StarterPlayerScripts and is it a local script?

1 answer

✓ Accepted answer

So there's two options I see here: either you're using a Server Script, in which case you'd need the PlayerAdded event of the Players Service, or you're using a Local Script and you can use the LocalPlayer variable of the Players Service to get the Client.

Also, I'd suggest using the player's UserId as this is completely unique for each player, as opposed to their UserName, but i'll show you both.

Server Script Example #1

game:GetService("Players").PlayerAdded:Connect(function(player)
	if player.UserId == 813309294 then
		-- Run Code
	end
end)

Server Script Example #2

game:GetService("Players").PlayerAdded:Connect(function(player)
	if player.Name == "UnrealTube" then
		-- Run Code
	end
end)

Local Script Example #1

local player = game:GetService("Players").LocalPlayer
if player.UserId == 813309294 then
	-- Script is Kept
else
	script:Destroy()
end

Local Script Example #2

local player = game:GetService("Players").LocalPlayer
if player.Name == "UnrealTube" then
	-- Script is Kept
else
	script:Destroy()
end

For the Local Scripts, I'm destroying the script if the player doesn't match the players you want the script to be given to. These lines should appear before any code is run.

You can use "or" to add other players, or iterate over a table containing the player's name / userid and check if the name / userid matches before continuing your code.

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