Why function runs when it shouldn't

why when i dont have enough clicks my function still runs if i click like 3-5 times?

game:WaitForChild("ReplicatedStorage").Remotes.ClickUpgrade.OnServerEvent:Connect(function(player)
	local UpgradePrice = game.ReplicatedStorage.Prices.ClickUpgradePrice
	local UpgradeCount = game.ReplicatedStorage.Upgrades.ClicksUpgrades
	wait(.1)
	if player.leaderstats.Clicks.Value > UpgradePrice.Value then
		else return end
		player.leaderstats.Clicks.Value -= UpgradePrice.Value
		UpgradeCount.Value += 1
		wait(1)
end)

dont look at

else return end

i was trying to fix that it didn’t change anything

Each call to the click handler function is effectively like a new coroutine, and you’ve got a wait(0.1) before you do the additions and subtractions. You can’t yield, otherwise multiple clicks will be handled before any math gets done.

so what i need to do? im new at scripting can’t understand what you mean

Start by deleting the wait(.1), see if you still have the problem.

yes there is still this problem

Instead of having >, add an equals sign next to it like so >= on line 5. This should take care of your issue.

if player.leaderstats.Clicks.Value >= UpgradePrice.Value then
1 Like

ohhhhh nooo this is gonna be like this (script from my previous game):

	if ups.Value == 0 and Money.Value >= 10 then
		Money.Value -= 10
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 1 and Money.Value >= 12 then
		Money.Value -= 12
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 2 and Money.Value >= 15 then
		Money.Value -= 15
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 3 and Money.Value >= 20 then
		Money.Value -= 20
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 4 and Money.Value >= 25 then
		Money.Value -= 25
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 5 and Money.Value >= 32 then
		Money.Value -= 32
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 6 and Money.Value >= 40 then
		Money.Value -= 40
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 7 and Money.Value >= 50 then
		Money.Value -= 50
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 8 and Money.Value >= 65 then
		Money.Value -= 65
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 9 and Money.Value >= 82 then
		Money.Value -= 82
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 10 and Money.Value >= 105 then
		Money.Value -= 105
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 11 and Money.Value >= 132 then
		Money.Value -= 132
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 12 and Money.Value >= 165 then
		Money.Value -= 165
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 13 and Money.Value >= 210 then
		Money.Value -= 210
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 14 and Money.Value >= 265 then
		Money.Value -= 265
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 15 and Money.Value >= 350 then
		Money.Value -= 350
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 16 and Money.Value >= 450 then
		Money.Value -= 450
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 17 and Money.Value >= 565 then
		Money.Value -= 565
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 18 and Money.Value >= 750 then
		Money.Value -= 750
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 19 and Money.Value >= 1000 then
		Money.Value -= 1000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 20 and Money.Value >= 1250 then
		Money.Value -= 1250
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 21 and Money.Value >= 1500 then
		Money.Value -= 1500
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 22 and Money.Value >= 1800 then
		Money.Value -= 1800
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 23 and Money.Value >= 2160 then
		Money.Value -= 2160
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 24 and Money.Value >= 2592 then
		Money.Value -= 2592
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 25 and Money.Value >= 3110 then
		Money.Value -= 3110
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 26 and Money.Value >= 3732 then
		Money.Value -= 3732
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 27 and Money.Value >= 4480 then
		Money.Value -= 4480
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 28 and Money.Value >= 5376 then
		Money.Value -= 5376
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 29 and Money.Value >= 6450 then
		Money.Value -= 6450
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 30 and Money.Value >= 7750 then
		Money.Value -= 7750
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 31 and Money.Value >= 9300 then
		Money.Value -= 9300
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 32 and Money.Value >= 11610 then
		Money.Value -= 11610
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 33 and Money.Value >= 13930 then
		Money.Value -= 13932
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 34 and Money.Value >= 16720 then
		Money.Value -= 16720
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 35 and Money.Value >= 20000 then
		Money.Value -= 20000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 36 and Money.Value >= 24000 then
		Money.Value -= 24000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 37 and Money.Value >= 28800 then
		Money.Value -= 28800
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 38 and Money.Value >= 34560 then
		Money.Value -= 34560
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 39 and Money.Value >= 41470 then
		Money.Value -= 41470
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 40 and Money.Value >= 50000 then
		Money.Value -= 50000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 41 and Money.Value >= 60000 then
		Money.Value -= 60000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 42 and Money.Value >= 72000 then
		Money.Value -= 72000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 43 and Money.Value >= 86400 then
		Money.Value -= 86400
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 44 and Money.Value >= 103680 then
		Money.Value -= 103680
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 45 and Money.Value >= 125000 then
		Money.Value -= 125000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 46 and Money.Value >= 150000 then
		Money.Value -= 150000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 47 and Money.Value >= 180000 then
		Money.Value -= 180000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 48 and Money.Value >= 216000 then
		Money.Value -= 216000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 49 and Money.Value >= 256000 then
		Money.Value -= 256000
		Multi.Value += 1
		ups.Value += 1
	elseif ups.Value == 50 and Money.Value >= 320000 then
		Money.Value -= 320000
		Multi.Value += 1
		ups.Value += 1

i dont want to be it like that again

That’s what for loops are. They are going to be your best friend at that!

so can u help me make loop here?:

game:WaitForChild("ReplicatedStorage").Remotes.ClickUpgrade.OnServerEvent:Connect(function(player)
	local UpgradePrice = game.ReplicatedStorage.Prices:WaitForChild("ClickUpgradePrice")
	local UpgradeCount = game.ReplicatedStorage.Upgrades:WaitForChild("ClicksUpgrades")
	
	if player.leaderstats.Clicks.Value > UpgradePrice.Value then
		player.leaderstats.Clicks.Value -= UpgradePrice.Value
		UpgradeCount.Value += 1
	end
end)

i will really appreciate that and will teach from this.
please