Hello, first thanks for reading this. Basically what I am trying to do is make it so when a player joins or respawns a GUI is cloned to there head and that part work's. But the while() function does not it stops working on 19 where it says.
if v.Character.Head.TimeGui.Label.Time.Value >= Highest_Time then
I don't want a straight change this to this. But a push in the correct direction would be appreciated. Thanks for reading this.
local Highest_Time = 0 local Highest_Player_Name game.Players.PlayerAdded:connect(function(Player) repeat wait() until Player.Character script:WaitForChild("TimeGui") script.TimeGui:Clone().Parent = Player.Character.Head end) game.Workspace.ChildAdded:connect(function(Child) repeat wait() until Child.Torso script:WaitForChild("TimeGui") script.TimeGui:Clone().Parent = Child.Head end) while wait(1) do for i,v in pairs (game.Players:GetPlayers()) do if v.Character.Head:FindFirstChild("TimeGui") then if v.Character.Head.TimeGui.Label.Time.Value >= Highest_Time then Highest_Time = v.Character.Head.TimeGui.Label.Time.Value Highest_Player_Name = v.Name print(v.Name) print(Highest_Time) end end end if Highest_Player_Name ~= nil then local Player = game.Players:FindFirstChild(Highest_Player_Name) Player.Character.Head.TimeGui.Label.TextColor3 = Color3.new(255,0,0) end end
There are two things off about lines 4 to 14.
First, if the script starts after a player has joined, then the PlayerAdded won't capture them. This will only matter when testing in Test Solo.
Second, you're repeating yourself. You should just use the CharacterAdded
event, instead of attacking all of the new children in the workspace.
function newCharacter(character) script:WaitForChild("TimeGui"):Clone().Parent = character:WaitForChild("Head") end -- Handle a new player function newPlayer(player) player.CharacterAdded:connect(newCharacter) if player.Character then newCharacter(player.Character) end end game.Players.PlayerAdded:connect(newPlayer) -- For anyone already playing (may be a problem in solo) for _, player in pairs(game.Players:GetPlayers()) do newPlayer(player) end
The script you had fails error checking in a few ways. Line 11 until Child.Torso
will error if Torso
doesn't exist. You need to use :FindFirstChild
to get nil
instead of an error.
You don't check for Character
and Head
existing in line 18
. You need to do that, or else you'll get an error when a player joins or when they die.
It's usually a bad idea to use names when you can just use the object itself. You should just save Highest_Player
, and then avoid the game.Players:FindFirstChild(Highest_Player_Name)
.