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

Trouble with if and elseif statements again, Help?

I'm making a speed meter gui that makes certain image labels only visible when a player reaches a certain speed. My problem is when the player slows down in speed the gui doesn't update until the player presses another button. I tried using elseif statements, but I got the same result.

local dude = script.Parent
local disguy = game.Players.LocalPlayer.PlayerGui.ScreenGui1:WaitForChild("SpeedCounterFrame")

dude.Running:connect(function()
if dude.WalkSpeed < 30
	then
	disguy.SpeedCounter1.Visible = false
end
end)

dude.Running:connect(function()
if dude.WalkSpeed > 30 then
	disguy.SpeedCounter1.Visible = true
end
end)
dude.Running:connect(function()
if dude.WalkSpeed < 60 then
	disguy.SpeedCounter2.Visible = false
end
end)

dude.Running:connect(function()
if dude.WalkSpeed > 60 then
	disguy.SpeedCounter2.Visible = true
end
end)

dude.Running:connect(function()
if dude.WalkSpeed < 90 then
	disguy.SpeedCounter3.Visible = false
end
end)

dude.Running:connect(function()
if dude.WalkSpeed > 90 then
	disguy.SpeedCounter3.Visible = true
end
end)

dude.Running:connect(function()
if dude.WalkSpeed < 6000 then
	disguy.SpeedCounterHighest.Visible = false
end
end)

dude.Running:connect(function()
if dude.WalkSpeed > 6000 then
	disguy.SpeedCounterHighest.Visible = true
end
end)

5 comments

1

Why do you have all those different ifs in multiple separate functions? Why not put them all in one function, instead of creating multiple for each to check the WalkSpeed? It doesn't make any sense.

2

That's what his question is for...

1

Try using the Changed event instead of the Running event.

0

Thank you @Percil

0

Running (and Changed) are events, so if they don't happen sometimes, neither will your function. Use loops.

1 answer

Try this.

local dude = script.Parent
local disguy = game.Players.LocalPlayer.PlayerGui.ScreenGui1:WaitForChild("SpeedCounterFrame")

dude:GetPropertyChangedSignal("WalkSpeed"):connect(function()
	if dude.WalkSpeed <= 30 then
		disguy.SpeedCounter1.Visible = false
	end
	if dude.WalkSpeed >= 30 then
		disguy.SpeedCounter1.Visible = true
	end
	if dude.WalkSpeed <= 60 then
		disguy.SpeedCounter2.Visible = false
	end
	if dude.WalkSpeed >= 60 then
		disguy.SpeedCounter2.Visible = true
	end
	if dude.WalkSpeed <= 90 then
		disguy.SpeedCounter3.Visible = false
	end
	if dude.WalkSpeed >= 90 then
		disguy.SpeedCounter3.Visible = true
	end
	if dude.WalkSpeed <= 6000 then
		disguy.SpeedCounterHighest.Visible = false
	end
	if dude.WalkSpeed >= 6000 then
		disguy.SpeedCounterHighest.Visible = true
	end
end)

The thing that caused you to fail is that Running fires directly after they start/stop, and if you have some sort of speed fading script, it stacks directly with this one, and fires at the exact time that this one does (assuming that the fading script uses Humanoid.Running, which WOULD be the most efficient way). With dude:GetPropertyChangedSignal("WalkSpeed"), it will fire every time that dude.WalkSpeed changes.

PS: Use the SAME function for stuff if you're gonna use the same function every time. It really helps to shorten up the code, as well as reduce lag!

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