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

Is there a way to use game.Players.Localplayer but in regular scripts?

I really need help with that as it is required for my script.

1 comment

0

Regular Scripts live on the server, but there's multiple players playing. Which one is the "local player"?

1 answer

✓ Accepted answer

It is a little more complicated to find the local player via global script, although it can still be achieved.

One way that the player can be found would be to do the following:

game.Players.PlayerAdded:connect(function(plr)
	-- do stuff
end)

In this case, 'plr' would be that local player.

In addition to finding the player by a PlayerAdded event, it can also be achieved via the Touched event:

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = hit.Parent:GetPlayerFromCharacter()
	end
end)

5 comments

0

Thanks!

0

Glad it helped! :D

0

I have one more question, how would I use it in an if statement that does this: ID = 1337 if ID == game.Players.LocalPlayer.UserId then

0

You're not necessarily getting the LocalPlayer. The LocalPlayer is only accessible in LocalScripts. What you are doing is utilizing an event that provides the player object. So you would need to find some way to get a hold of the player through what ever event you need to use. https://scriptinghelpers.org/blog/common-mistakes#ServerScripts

0

can it be found in a clicked event

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