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

Over head GUI not working as planned, advice?

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

5 comments

1

Might it have anything to do with the potential problem of Character being nil for a Player?  Is there an error message of any kind that could help to debug it?

0

Thanks, I got an idea of fixing it, if that is indeed the problem.

0

That didn't seem to work. I added a " while repeat wait() until v.Character" and I have the same problem.

0

How it the script behaving? Is the script erroring? If so what is the message?

0

That's the weird thing it is behaving fine. No errors, messages, warnings. And now I ran the code again after adding prints after each if and it runs through everyone of them no problem. That's why I don't see why it won't work.

1 answer

✓ Accepted answer

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

2 comments

0

This solved most of my problem's, but there is still one. It won't update the value Highest_Time and so all players GUIs will be red even if there value is not the highest. But anyways thanks for all the help.

0

You never reset Highest time, nor do you ever change any color back to what it was. Just define the variable in the loop instead of before it.

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