I have this code:
local player = game.Players.LocalPlayer
local unitFrame = script.Parent
local buttona = unitFrame.buttonA
local sprinting = true
unitFrame.Title.Text = "Perks"
local function onButtonAClick()
unitFrame.Title.Text = "perks"
end
local function sprintButton()
if sprinting == false then
sprinting = true
player.Character.Humanoid.WalkSpeed = 20
unitFrame.buttonA.Text = "strinting"
end
if sprinting == true then
sprinting = false
player.Character.Humanoid.WalkSpeed = 16
unitFrame.buttonA.Text = "walking"
end
end
buttona.MouseButton1Click:connect(sprintButton)
What I am trying to do is make a sprinting toggle program. The only problem is that it will work once then not work at all. I can click it and it changes the text then when I click it again it does nothing. I want it to be able to work every time you press it.
In the function you test 2 if statements. If sprinting is false, and if it it is true.
The key to understanding your problem is that your if statements counteract eachother.
Let's walk through this, first it detects if it's false. No, sprinting is not false. Then if checks if it's true. It is! So it sets it to false.
Great so far, right? Well here's your problem. When you try it again, sprinting is false. So the first if statement is run, and it sets it to true. But then, ANOTHER if statement is run immediately after. Since it's now true, this runs, setting it to false. It appears nothing happened.
Your solution? Take out the end to the first if statement, and replace the second if to an elseif. This way once one statement evaluated to true, it won't evaluate the next.