UI not updating at all sometimes?

, ,

Hello, this is my first ever post on the devforum, so sorry if I break a rule or get something wrong. I have around 7 months of programming experience.
I’m creating a game where players “push” a cube to activate a random event, and in that game I have a stats bar that shows your health, your speed, and your jump-power. I’m not the best at designing UI, so sorry if it looks bad, but my issue is that sometimes when I respawn… it just doesn’t do anything. I think it may be an issue with me using the CharacterAdded event. I’ve recorded a video below to showcase the issue.

I’d like to know any potential issues that could revolve around it. Thanks for reading! :slight_smile:

1 Like

May we see the code that causes the issue?

My code’s probably pretty messy since I’m pretty tired right now, but here’s the script. Sorry if it’s bad;
(player is defined)

player.CharacterAdded:Connect(function(character)
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		local playerGUI = player.PlayerGui
		local statsGui = playerGUI:WaitForChild("CharacterGui")
		local entireFrame = statsGui:FindFirstChild("HealthBar")
		local statsFrame = entireFrame:FindFirstChild("StatsFrame")
		local healthBar = entireFrame:FindFirstChild("HealthFrame")
		local healthText = statsFrame:FindFirstChild("Health"):FindFirstChild("HealthText")
		local speedText = statsFrame:FindFirstChild("Speed"):FindFirstChild("SpeedText")
		local jumpText = statsFrame:FindFirstChild("Jump"):FindFirstChild("JumpText")
		oldHealth = humanoid.Health
		
		humanoid.HealthChanged:Connect(function(newHealth)
			local maxHealth = math.round(humanoid.MaxHealth)
			local health = math.round(newHealth)
			healthBar:TweenSize(UDim2.new(health / maxHealth, 0, 1, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Sine,0.2)
			healthText.Text = health .. "/" .. maxHealth

			if newHealth > oldHealth then
				print("Healed")
				for i = 0, 12 , 1 do
					healthText.Parent.Position += UDim2.new(math.random(-1,1) / 100,0,math.random(-2,2) / 100,0)
					task.wait(0.01)
					healthText.Parent.Position = UDim2.new(0.049, 0,-0, 0)
				end
				healthText.Parent.Position = UDim2.new(0.049, 0,-0, 0)
			end
			if oldHealth > newHealth then
				print("Took damage")
				for i = 0, 12 , 1 do
					entireFrame.Position += UDim2.new(math.random(-2,2) / 100,0,math.random(-2,2) / 100,0)
					task.wait(0.01)
					entireFrame.Position = UDim2.new(0.739, 0,0.886, 0)
				end
				entireFrame.Position = UDim2.new(0.763, 0,0.885, 0)
			end
			oldHealth = health
		end)
		humanoid.Changed:Connect(function(property)
			if property == "WalkSpeed" then
				speedText.Text = math.round(humanoid.WalkSpeed)
				for i = 0, 12 , 1 do
					speedText.Parent.Position += UDim2.new(math.random(-1,1) / 100,0,math.random(-2,2) / 100,0)
					task.wait(0.01)
					speedText.Parent.Position = UDim2.new(0.384, 0,0.052, 0)
				end
				speedText.Parent.Position = UDim2.new(0.384, 0,0.052, 0)
			else
				if property == "JumpPower" then
					jumpText.Text = math.round(humanoid.JumpPower)
					for i = 0, 12 , 1 do
						jumpText.Parent.Position += UDim2.new(math.random(-1,1) / 100,0,math.random(-2,2) / 100,0)
						task.wait(0.01)
						jumpText.Parent.Position = UDim2.new(0.638, 0,0.052, 0)
					end
					jumpText.Parent.Position = UDim2.new(0.638, 0,0.052, 0)
				end
			end
		end)
	end
end)
1 Like

I think the issue is that the player exists before the CharacterAdded event, meaning that the CharacterAdded event is still waiting for the character, even though it already exists. I recommend also checking if the player already exists, to then do the same thing. Here’s how it should work:

local function healthBarUpdater()
	-- do code here
end

player.CharacterAdded:Connect(function(character)
	local human = character:WaitForChild("Humanoid")

	healthBarUpdater(player, character)
end)

if player.Character then
	local human = player.Character:WaitForChild("Humanoid")

	healthBarUpdater(player, player.Character)
end

Awesome! I just tried it, and it works 100% of the time (or at least 100% of the 5 times i tested resetting)! Thanks man, you just saved me a bunch of time. :smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.