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

How do i get a player into a server script?

i have been trying to do it for a while now but i cant find out how

1 comment

0

Include your scripts in a code block and explain your questions in more detail.

3 answers

There are various ways to get the Player in a ServerScript:

1. Using the PlayerAdded event.

You can make up a function called with this event, which you can grab the player from the parameter provided by the event.

game:GetService("Players").PlayerAdded:Connect(function(player)
	-- code
end)

2. Getting the player when having the character.

If you have the character you can also get the player with this built-in method in the Players' Service.

local player = game:GetService("Players"):GetPlayerFromCharacter(character)

There are more ways to get the player in specific contexts, but I'd recommend to use PlayerAdded. Have a good day!

1 comment

0

Or loop trough the player objects, that's actually one of the best ways

Use remote event Create a remoteevent in ReplicatedStorage and name it as in the event variable

player = game.Players.LocalPlayer
local event = "name of remote event here"
player.CharacterAdded:Connect(function() -- Get it on characteradded
	game.ReplicatedStorage:FindFirstChild(event):FireServer()
end)

. On a server script:(Put in ServerScriptService)

event = "name of remote event here"
game.ReplicatedStorage:FindFirstChild(event).OnServerEvent:Connect(function(player)
print(player)
end)

You can use PlayerAdded and add the player to a table:

local playerTable = {}

game.Players.PlayerAdded:connect(function(player)
	for i, v in pairs(playerTable) do -- a loop to check if the player is already in the table
		if v == player then
			return
		else
			table.insert(playerTable, player) -- insert player in the table
		end
	end
end)

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