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.

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)

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 5 years ago
Edited 5 years ago

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!

Ad

Answer this question