For loop cannot change values

My goal is to have a script that perpetually goes through a table (keys being players and values being the values of interest) and halves any entries that are above 0.1, setting all else to 0 (this is a mechanism to reduce the recoil inaccuracy of guns after they fire).

Currently my server script looks like this:

game["Run Service"].Heartbeat:Connect(function()
	for k,v in pairs(shared.RecoilTable) do
		if v > 0.1 then
			v *= 0.5
			print("The recoil SHOULD be halved")
		else
			v=0
			print("The recoil SHOULD be set to 0")
		end
	end
end)

The prints do fire at the correct times whenever I manually increase or decrease the recoil for each player, but for some reason it simply refuses to edit v, weather that be to multiply it by 0.5 or set it to 0, V keeps staying unchanged.
Does anyone know why the script seems to just not be listening to me.

You shouldn’t be using v *= 0.5, I believe the correct usage is v = v * 0.5. And the problem also could be that you should be doing shared.RecoilTable[k] = shared.RecoilTable[k] * 0.5 either of those.

*= doesnt work in lua, but is a short cut for x = x * something in luau

the problem is that you are changing the variable v which is a copy of the value in the table, not the actual value

change the actual value by doing

shared.recoilTable[k] *= 0.5

2 Likes

Initially i’ve had issues trying to get operator= to work, in some personal cases it doesn’t always work for me.

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