How do I reference a player I don't know the name of?

Hi Developers! I am pretty new to scripting so I couldn’t figure out how to reference a player in an obby I am making. I have a code with the leaderstats value “Stage”. I want to reference my player and basically say something along the lines of "if game.Players.(Player name I don’t know that I want to know how to reference even though I don’t knwo their name).leaderstats.Stage.Value = 2 then (Insert effect here) then a block will disappear blocking them from stage 2. This is how I made the leaderstats:

players.PlayerAdded:connect(function(plr)

local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr

local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = stats

local data = saveDataStore:GetAsync(plr.UserId)

if data then

	print(data.Stage)

	for _,stat in pairs(stats:GetChildren()) do

		stat.Value = data[stat.Name]

	end

else

	print(plr.Name .. "has no data")

end

Please help. Thanks!

2 Likes

You can put this in your on PlayerAdded event

stage.Changed:Connect(function()
if stage.Value == 2 then
-- Your Code Here
end
end)

Is there a way I can transfer that to a local script because my script with the on PlayerAdded event is a regular script and I want only people who are on stage 2 to be able to see the effects of my code.

To start off you could use a PlayerAdded event. To do this just do:

game:GetService(“Players”).PlayerAdded:Connect(function(player)
– Code here
end)

Also sorry if my format isn’t correct. I just got the member role today.

Yes you can something like this in a local script:

local player = game.Players.LocalPlayer

local leaderstats = player:WaitForChild("leaderstats")

local stage = leaderstats:WaitForChild("Stage")

stage.Changed:Connect(function()
if stage.Value == 2 then
-- Your Code Here
end
end)
1 Like

If your script is correct, then it should show only for those who are on stage 2. The client will not be needed.

They just want the effects to happen on the local player’s screen I believe

1 Like

Yes, that is what I want. I only want people who are on stage 2 or above to experience the effects. That is another question I have, how do I write the code so that people who are above stage 2 can also experience the effects?

Change the equality operator == to less or equals than operator >= if that’s what you’re looking for (as you said if it’s stage 2 or above).

I gave you the code here How do I reference a player I don't know the name of? - #5 by AWhale_OfATime

Wouldn’t you just be able to do:

if game.Players:FindFirstChild(plr.Name).leaderstats.Stage.Value == 2 then

I put the code you wrote in a local script and put the local script inside the block I want the effects to happen on and just referenced it as script.parent but it doesn’t work. Here is the code:

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local stage = leaderstats:WaitForChild("Stage")
stage.Changed:Connect(function()
if stage.Value == 2 then
script.Parent.Part.CanCollide = false
script.Parent.Part.Transparency = 1
script.Parent.Sign.SurfaceGui.SIGN.TextTransparency = 1
end
end)

Are you sure you are changing the value on the server?

Yes, for me the stage value is at 2.

Are you getting any errors in the output?

No, I am not getting any errors. I checked.

Try adding print statements to debug.

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
print("found leaderstats")
local stage = leaderstats:WaitForChild("Stage")
print("found stage")
stage.Changed:Connect(function()
print("stage changed")
if stage.Value == 2 then
print("stage = 2")
script.Parent.Part.CanCollide = false
script.Parent.Part.Transparency = 1
script.Parent.Sign.SurfaceGui.SIGN.TextTransparency = 1
end
end)
2 Likes

Sorry I took a while to reply! Nothing seems to print, indicating it might be something with local player.

Sorry for the late reply, that means you’re not making a folder in the player called ‘leaderstats’, check your code which does that.

1 Like