I only started scripting a couple of days ago so please don’t judge me.
I’m working on a game and in the game there are upgrades. What I want to achieve with this LocalScript is everytime I click the ImageButton (the parent of this script) the character will gain +1 speed after clicking it once, and when I click it again the character will gain +1 speed again but the price gradually goes up. And I have a rough idea of how the script should work. When I click once only the 1st “for loop” should run then when I click again only the 2nd “for loop” should run then the last for loop should run 8 times because that would be the max (+10 speed) upgrade. But I don’t think that “for loops” should be used in the script at all I just went with it because this is as far as my knowledge goes in scripting. The issue is that all 3 “for loops” run at the same time when I click the ImageButton. Hopefully I was understandable.
This is my current script:
local Player = game.Players.LocalPlayer
local Character = game.Players.LocalPlayer.Character
local UpgradePoint = Player.Stats.UpgradePoint
for i = 1,2,1 do
script.Parent.MouseButton1Click:Connect(function()
if UpgradePoint.Value >= 1 then
UpgradePoint.Value -= 1
script.Parent.Parent.SpeedText.Text = "+1 Speed for 2 UP"
Character.Humanoid.WalkSpeed +=1
end
end)
end
for o = 1,2,1 do
script.Parent.MouseButton1Click:Connect(function()
if UpgradePoint.Value >= 2 then
UpgradePoint.Value -= 2
script.Parent.Parent.SpeedText.Text = "+1 Speed for 3 UP"
Character.Humanoid.WalkSpeed +=1
end
end)
end
for p = 1,9,1 do
script.Parent.MouseButton1Click:Connect(function()
if UpgradePoint.Value >= 3 then
UpgradePoint.Value -= 3
Character.Humanoid.WalkSpeed +=1
end
end)
end
Also a picture of where this is located:
