Adding a value to i

Hi, I am trying to add a number to i but it’s not working, does anyone know how I can fix this?

for i = length, 0, -1 do		
game.ReplicatedStorage.Events.TaskCompleted.OnServerEvent:Connect(function(player, TaskName, Innocent, Murderer)
			if Innocent == true then
				print("innocent true")
				i = i - 15
			else
				print("murderer true")
				i = i + 20
			end
		end)
end

It does print but doesn’t add or subtract

1 Like

It didn’t change anything, which is odd because I’d thought that length would fix the issue.

1 Like

AFAIK you can’t change the i in a for loop, therefore you can use a while loop to achieve the same thing.

local i = length
while i >= 0 do
    --do stuff here
    i -= 1
end

And why do you need to OnServerEvent it multiple times? I’m confused with what you are doing here.
And also you can use value += increment and value -= increment.

1 Like

That’s just being used to fire after a task is done. So I just do I -= 15?

2 Likes

Yes, but you will need to use a while loop for this though.

1 Like

When I did that in a while loop, my time went down by 5 minutes, how do I fix that?

1 Like

You never stated anything about time, can you please elaborate on the matter?

1 Like

The default start time is 600 seconds, if the event is fired, it’ll go up or down by an extraordinary time when i just need it to increase or decrease by 20 or 15 seconds respectively

2 Likes

Ah, so you are connecting a new OnServerEvent event every time the while loop runs. Here’s the fix:

local i = length
game.ReplicatedStorage.Events.TaskCompleted.OnServerEvent:Connect(function(player, TaskName, Innocent, Murderer)
    if Innocent == true then
        print("innocent true")
        i -= 15
    else
        print("murderer true")
        i += 20
    end
end)

while i >= 0 do
    wait(1) -- Hope I am not wrong about this
    i -= 1
end
3 Likes