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

Help with changing WalkSpeed if gotten a certain # value?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

I have a ButtonGui and when I click it a textlabel counts up by 1's every click. When I click the button 1 time, the walkspeed changes to 1 but if you click it 20 times, walkspeed does not change :(

Plr = game.Players.LocalPlayer
I = Instance.new("IntValue")
I.Parent = Plr
TextL = Plr.PlayerGui.Clicker.TextLabel

Plr.Character.Humanoid.WalkSpeed = 0


I.Changed:connect(function()
    TextL.Text = "Clicks: " ..I.Value
end)

script.Parent.MouseButton1Click:connect(function()
    I.Value = I.Value + 1
end)

--Problem is below.

while wait(.1) do
    if I.Value == 1 then
        Plr.Character.Humanoid.WalkSpeed = 1
    end
end

while wait(.1) do
    if I.Value == 20 then
        Plr.Character.Humanoid.WalkSpeed = 20
    end
end

2 answers

Log in to vote
1
Answered by 8 years ago

The second while loop never starts because the first never stops and therefore it never checks if the value is 20. You could either use one while loop and elseif or just check the value after you update it.

while wait(.1) do
    if I.Value == 1 then
        Plr.Character.Humanoid.WalkSpeed = 1
    elseif I.Value == 20 then
        Plr.Character.Humanoid.WalkSpeed = 20
    end
end

Or

script.Parent.MouseButton1Click:connect(function()
    I.Value = I.Value + 1

    if I.Value == 1 then
        Plr.Character.Humanoid.WalkSpeed = 1
    elseif I.Value == 20 then
        Plr.Character.Humanoid.WalkSpeed = 20
    end
end)

You also could use the Changed event.

Ad
Log in to vote
1
Answered by
Im_Kritz 334 Moderation Voter
8 years ago

You do not need to create an IntValue for your walkspeed, because you can get the walkspeed directly off the humanoid.

The problem is your code is written so that it changes ONLY on 1 and 20. Not any of the values in between.

I changed it so that on click, the code changes your walkspeed and your textlabel spontaneously.

local Plr = game.Players.LocalPlayer
local TextL = Plr.PlayerGui.Clicker.TextLabel

Plr.Character.Humanoid.WalkSpeed = 0

script.Parent.MouseButton1Click:connect(function()
    Plr.Character.Humanoid.WalkSpeed = Plr.Character.Humanoid.WalkSpeed  + 1
    TextL.Text = "Clicks: " ..Plr.Character.Humanoid.WalkSpeed
end)
0
The intvalue is for the # of clicks. FiredDusk 1466 — 8y
0
That is true, but if the number of clicks is the same as your walkspeed, you don't really need to make it. Im_Kritz 334 — 8y

Answer this question