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

Reference a player in a server script?

Hey,

I have a LocalScript which uses

local player = game.Players.LocalPlayer

However I'm firing a remote event and the server script doesn't allow LocalPlayer.

How can I get the same result within the server script so that I can make changes to humanoid when I fire the server?

Thanks

2 comments

0

You can just make changes through the local script, I just did it and it works...

0

But it only makes the changes locally. If I log on my Son's account on the other laptop I can't see the effects on his character but he can and vice versa, I can't see the effects on my character from the other screen. It needs to be done through the server script

2 answers

✓ Accepted answer

If you fire a remote event from the client to the server, the first argument is automatically set to the player that fired the event. For example:

Local script:

remote:FireServer()

Server Script:

remote.OnServerEvent:Connect(function(plr)
	--plr will be the player who fired
	local humanoid = plr.Character.Humanoid
end)

1 comment

0

oh my gosh, I'm an idiot. I was doing plr.Humanoid and couldn't figure out why it wasn't working. Thank you very much Mattscy :)

If you want to get a reference to a player when they join the game in a server Script, you can do so like this:

game.Players.PlayerAdded:Connect(function(player) 
    print(player.Name.." has joined the game")

    -- Let's modify the character each time the player spawns
    player.CharacterAdded:Connect(function(character)
        -- This function will run as soon as the character model is available
        -- Sometimes, it runs before the body parts have been added to the
        --    character model, so we need to :WaitForChild("Head") instead
        --    of just index .Head to avoid a nil reference.
        local head = character:WaitForChild("Head")

        -- This color change will be visible to everyone
        head.BrickColor = BrickColor.Green()
    end)
end)

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