I need help with a script

Hi, im making a system where the player has to spam E, its very simple the way i wanna do it but there is something not working and its the fact that is not subtracting the value
take a look at my script:

local clickChain = game.ReplicatedStorage.Chain

if clickChain then
	if clickChain.Value > 0 then
		while clickChain.Value > 0 do
			wait(1)
			clickChain.Value = clickChain.Value - 1
		end
	elseif clickChain.Value >= 21 then
		print("SUCCESSSS")
	end
end

this bassically detects if the value is higher than 0 and subtracts 1 value every second, but its not doing so, the value is serversided aswell as this script. Does anyone know why?
The names are correct so dont worry i quatriple checked lol. Any help is appreciated

4 Likes

Not sure if this is part of the not subtracting problem, but the code within the condition “if clickChain.Value >= 21” will never run since that condition will never return true, as the condition is only checked when clickChain.Value <= 0.

1 Like

Thanks for a response, i will check

1 Like

Seperate the 2 if statements rather than using elseif

1 Like

That didnt work as i already tried that, but this is a better way of showing: https://gyazo.com/35396a239d57ec64fdfb6cf40aaf29c7

1 Like

Sorry, Im a bit brain dead xD

What you need to do is have the loop OUTSIDE of the check, not inside :slight_smile:

1 Like

try clickChain.Value -= 1, might work

also why do you need the if statement if the loop already checks it

1 Like

You know clickchain.Value = clickchain.Value - 1 means the exact same rthing

1 Like

Literally the same thing as x = x - 1

1 Like

How would that look like because how? I dont want to get negative values for example -2, -5 etc etc

1 Like

While true do:
If clicks >= max then
Run your code
End
If clicks > 0 then
Clicks -= 1
End
Wait(1)
End

Im on my phone so sorry for the scuffed format

local clickChain = game.ReplicatedStorage.Chain

while true do
	if clickChain.Value > 0 then
		while wait(1) do
			clickChain.Value -= 1
		end
	end
	
	if clickChain.Value >= 21 then
		print("Succes")
	end
	wait(1)
end

i made it this, thanks alot

1 Like

No problem, just contact me if you need any more help :slight_smile:

Yeah so it doesnt print “Succes”

Dont add nested loops, its getting stuck in the 8nside loop and not breaking out

1 Like

I have no idea what u mean… Can you explain it more clear ifyk what i mean?

You only need one loop, any nested loops will have unforseen consequences. Just have one loop, inside the loop first check if your value is over the threshold, elseif subtract your value by 1. After the checks wait(1).

If this doesnt work, it may be a seperate issue, is your value actually increasing?

the increasing spam E thing works perfectly fine just how i wanted it, i just want it to run code when the value goes over 21 if it does then run code, and in this example i wanna use print as to kinda like debug the thing

i just tested your code in studio and it worked fine, are you sure the script isn’t in a local script?
anyway if it still doesn’t work maybe try this?

for i = 1, clickchain.value do