Need help with my ImageButton script

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:
location

Are you maybe looking for something similar to this?

local Player = game.Players.LocalPlayer
local Character = game.Players.LocalPlayer.Character
local UpgradePoint = Player.Stats.UpgradePoint

local stage = 1 -- current stage

UPCostincrement = 1 	-- This tells us, per stage, how much more UP Cost we add. So for stage 2, 1UP, stage 3, 2UP and so on.
						--You can increase this to 2, so for stage 2, 2UP, stage 3, 4 UP and so on.
WalkSpeedIncrement = 1	-- This tells us, per stage, how much we increase the WalkSpeed

script.Parent.MouseButton1Click:Connect(function()
	local UPCost = UPCostincrement * stage
	
	if UpgradePoint.Value >= UPCost then
		UpgradePoint.Value -= UPCost
		script.Parent.Parent.SpeedText.Text = "+"..tostring(WalkSpeedIncrement).. "Speed for ".. tostring(UPCost) .." UP"
		Character.Humanoid.WalkSpeed +=WalkSpeedIncrement
		stage = stage + 1
	end
end)

Yes that works perfectly fine! Thank you very much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.