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

How would I locate a player in my game from a server script?

I am wanting to locate a player. The player is not specific and could be anyone who joins. Currently I am using:

local character = game.Workspace.Player
local player = game.Players:GetPlayerFromCharacter(character)

However, I am greeted with this error:

Player is not a valid member of Workspace

It is currently 3am and I am having a dumb moment. So could someone point out my mistake. Thanks :)

1 comment

0

It is running from a server script.

1 answer

✓ Accepted answer

###You cannot use LocalPlayer in a Script. You can get the player in many ways, but for this example I will use PlayerAdded.

game:GetService("Players").PlayerAdded:Connect(function(plr)
    print(plr, "has joined the game") 
    -- plr is your player, you can access things like you would with LocalPlayer
end)

###Here are a few events/callbacks that pass the player by default as a parameter:

  • MouseClick
  • PlayerRemoving
  • DialogChoiceSelected
  • OnServerEvent
  • OnServerInvoke (callback)
  • PromptPurchaseFinished
  • PromptGamePassPurchaseFinished

###I believe there are a few more, but these are a few to list.

3 comments

0

do not spoon feed scripts, he is not going to learn like this

0

there was an attempt from OP tho

0

no excuses, every time someone else gives a script even with an attempt from OP, you get all angry and downvote, but when you do it, its perfectly fine

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