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
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
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}
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)