Player name not storing to a variable

This is the script, it should be working fine, but workspace.Curent_player changes to nil after the playeradded event ends. player_name does not store it after the script either

local rp = game:GetService("ReplicatedStorage")
local ps = game:GetService("Players")
local INK = game.Workspace:WaitForChild("Ink!Sans")
local player_name 

ps.PlayerAdded:Connect(function(plr)
	player_name = plr.Name
	workspace:WaitForChild("Current_Player").Value = plr.Name
end)

-- animate:
local animator = require(INK.Script_Animate)

while wait(1) do
	animator["Sitting_1_Anim"]:Play()
	wait(animator["Sitting_1_Anim"].Length)
end

local get_distance = function(one,two)
	return (one.Position - two.Position).magnitude
end

Use plr.userId instead

Summary

charrrrrrrrs

Tried that, still becomes nil after the playeradded event ends

Are you sure Current_Player is a stringValue?

The name of a player that has just joined has to be a string value

Yes, that’s why I’m asking you if you are sure you are using a stringValue.

How are you checking whether the value was stored? It could be something there and not in the function that stores it.

No, just a variable that collects a name

The value is stored perfectly fine after [workspace:WaitForChild(“Current_Player”).Value = plr.Name], but after the function ends it turns into a nil value

How are you checking this? Can you show us the code?

My mistake, I thought the error was that Current_Player was not storing the value

-- updated excerpt
ps.PlayerAdded:Connect(function(plr)
	player_id = plr.UserId
	workspace:WaitForChild("Current_Player").Value = plr.Name
	print(workspace.Current_Player.Value)
end)
print(workspace.Current_Player.Value)

the first print runs (memedealer2111)
the second runs nil

Okay, the print thats inside the function will print the player’s name
The print that is outside the function will not print the player’s name and will print nil because the variable “player_id” has not been assigned to yet.

Edit: sorry, i read that wrong

the print outside of the function will print nil because workspace.Current_Player.Value has not been assigned to yet

I print the same thing both times, which is not the player_id, its the name of the player that I store in a string variable. The same thing happens when I print player_id variable {oh I see}

Try out this:

local rp = game:GetService("ReplicatedStorage")
local ps = game:GetService("Players")
local player_id 


-- updated excerpt
ps.PlayerAdded:Connect(function(plr)
	player_id = plr.UserId
	workspace:WaitForChild("Current_Player").Value = plr.Name
	print(workspace.Current_Player.Value)
end)

--This will print everytime the value changes
workspace.Current_Player.Changed:Connect(function()
	print(workspace.Current_Player.Value)
end)
1 Like
ps.PlayerAdded:Connect(function(plr)
	workspace:WaitForChild("Current_Player").Value = plr.Name
	task.wait(.1)
	player_name = workspace.Current_Player.Value
	
	print(player_name)
end)

this works

chaarrrrrrs

This would work even without the wait btw

He tries to print it outside of the function, he already made the first print work

Changed to this

-- updated excerpt
ps.PlayerAdded:Connect(function(plr)
	player_id = plr.UserId
	workspace:WaitForChild("Current_Player").Value = plr.Name
	print(workspace.Current_Player.Value)
end)

--This will print everytime the value changes
workspace.Current_Player.Changed:Connect(function()
	print(workspace.Current_Player.Value)
end)
print(workspace.Current_Player.Value)

it prints the name right in both functions, outside of that it still prints nothing.

You cannot print it outside because the print is executed before playeradded runs.

1 Like