How to get a player in a server script

Hello there, i am trying to get a player in a server script but dont know how. I have tried sending a remote to the server and the argument is the player i have tried using the player added event but its not working. Is there any other ways?

4 Likes

The best way to do this is using the PlayerAdded event:

local PlayerINeed

game.Players.PlayerAdded:Connect(function(Player)
	PlayerINeed = Player
end)

If you’re still having issues, I need to know what you need the Player for.

1 Like

You need a reference to the player, ex from a PlayerAdded event.

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(Player)
	print(Player .. ' has joined the server.')
end)
1 Like

A player added event breaks my script if the script I’m doing is in it

How does it break?
…

I don’t know how it does…, It just breaks so I won’t use it…

What is your current code?
…

When you say it “breaks”, you mean the script won’t run just cause of the event?

It’s when a player is added it gets the parameter player then in the same bit of code in the player added event it has if a script errors then it clones a text label into a player

Show us your current code and we can see what you mean.

1 Like

Basically. The script continues but that part of code breaks

I possibly think that since it is in a player added event whenever a player joins it checks for code errors then when a script does error it won’t clone because it needs another player to be added to check for errors.

We can’t help you without any code.

1 Like

[Cannot be empty. aaaaaaaaaaaa]

Alright then it will take me about 3 minutes because I’m on tablet.

1 Like

I will tell u the code in the morning since I’m going to bed now.

Here is my script:

local ScriptContext = game:GetService(“ScriptContext”)
game.Players.PlayerAdded:Connect(function(Player)
ScriptContext.Error:Connect(function(message, trace, script)
local PlayerCloneThingy = game.ReplicatedStorage.PlayerCloneThingy
local NewThingy = PlayerCloneThingy:Clone()
NewThingy.Parent = Player
end)
end)

but its not working because i think when a player gets added it runs the error check and when a error does come by it wont run it because it needs a player to be added.

Your reason for this breaking is because you’ve added both threads into the same thread.

Here is a fix ;

--//Services

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ScriptContext = game:GetService("ScriptContext")

--//Variables 

local PlayerCloneThingy = ReplicatedStorage:WaitForChild("PlayerCloneThingy")

--//Events

Players.PlayerAdded:Connect(function(Player)
	local NewThingy = PlayerCloneThingy:Clone()
	NewThingy.Parent = Player
end)

ScriptContext.Error:Connect(function(message, trace, script)
	--do your thing here
end)

Tried it out, should work just fine. Hope it helps!

Would this clone PlayerCloneThingy into the Player when a script errors?, Because thats what i want to try and do.

No, that’s not what you want to do for catching an error. You can do pcall, or xpcall. That’s what you’re looking for to catch an error in a script, the ScriptContext errors are completely different, read this thread about pcall,xpcall, ect.

And this thread about what ScriptContext error really does;