Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Trouble with if and elseif statements again, Help?

Asked by 7 years ago
Edited 7 years ago

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.

01local dude = script.Parent
02local disguy = game.Players.LocalPlayer.PlayerGui.ScreenGui1:WaitForChild("SpeedCounterFrame")
03 
04dude.Running:connect(function()
05if dude.WalkSpeed < 30
06    then
07    disguy.SpeedCounter1.Visible = false
08end
09end)
10 
11dude.Running:connect(function()
12if dude.WalkSpeed > 30 then
13    disguy.SpeedCounter1.Visible = true
14end
15end)
View all 50 lines...
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. TheeDeathCaster 2368 — 7y
2
That's what his question is for... RubenKan 3615 — 7y
1
Try using the Changed event instead of the Running event. Perci1 4988 — 7y
0
Thank you @Percil JoeRaptor 72 — 7y
0
Running (and Changed) are events, so if they don't happen sometimes, neither will your function. Use loops. cabbler 1942 — 7y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Try this.

01local dude = script.Parent
02local disguy = game.Players.LocalPlayer.PlayerGui.ScreenGui1:WaitForChild("SpeedCounterFrame")
03 
04dude:GetPropertyChangedSignal("WalkSpeed"):connect(function()
05    if dude.WalkSpeed <= 30 then
06        disguy.SpeedCounter1.Visible = false
07    end
08    if dude.WalkSpeed >= 30 then
09        disguy.SpeedCounter1.Visible = true
10    end
11    if dude.WalkSpeed <= 60 then
12        disguy.SpeedCounter2.Visible = false
13    end
14    if dude.WalkSpeed >= 60 then
15        disguy.SpeedCounter2.Visible = true
View all 29 lines...

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!

Ad

Answer this question