IntValue subtracts by 1-3 when it should just subtract by 1

I have an IntValue to store how many batteries the player has, but when I try to subtract 1 from this value, it seems to subtract randomly from 1-3

Video
robloxapp-20241014-0223346.wmv (356.6 KB)

Here are the scripts that I believe are possibly causing the issue

LocalScript on the Flashlight

local replicatedStorage = game:GetService("ReplicatedStorage")

local light:Tool = script.Parent
local handle:BasePart = light:WaitForChild("Handle")
local spotLight:SpotLight = handle:WaitForChild("SpotLight")
local char:Model = light.Parent
local cam:Camera = workspace.CurrentCamera
local batteries:IntValue = light:WaitForChild("Batteries")
local battery:NumberValue = light:WaitForChild("Battery")
local events:Folder = replicatedStorage:WaitForChild("Events")
local useBattery:RemoteEvent = events:WaitForChild("UseBattery")
local remoteBattery:RemoteEvent = events:WaitForChild("Battery")
local toggleLight:RemoteEvent = events:WaitForChild("ToggleLight")

light.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if battery.Value > 0 then
			toggleLight:FireServer(spotLight)
		end
	end)
end)

light.Unequipped:Connect(function()
	toggleLight:FireServer(spotLight, false)
end)

local function ReplaceBattery(newBattery:number, newBatteries:number)
	if batteries.Value > 0 then
		remoteBattery:FireServer(battery, newBattery)
		useBattery:FireServer(batteries, newBatteries)
	end
end

battery:GetPropertyChangedSignal("Value"):Connect(function()
	if battery.Value == 0 then
		toggleLight:FireServer(spotLight, false)
		wait(1)
		ReplaceBattery(100, -1)
	end
end)

while task.wait() do
	if spotLight.Enabled then
		remoteBattery:FireServer(battery, -0.05)
	end
end

ServerScript in ServerScriptService for changing the IntValue

local replicatedStorage = game:GetService("ReplicatedStorage")

local events:Folder = replicatedStorage:WaitForChild("Events")
local useBattery:RemoteEvent = events:WaitForChild("UseBattery")

useBattery.OnServerEvent:Connect(function(player:Player, batteries:IntValue, amount:number)
	batteries.Value += amount
	batteries.Value = math.clamp(batteries.Value, 0, 5)
end)

I believe the fact that you are not immediately using the clamp is causing issues. If you make it simply

batteries.Value = math.clamp(batteries.Value + amount, 0, 5)

does it fix the issue?

I suspect the issue is occuring because of lag between the client and server, resulting in the client sending multiple FireServer events before the game switches off the light. This causes the batteries value to change to batteries.Value + amount, then quickly changes back to 0 (because of the clamp). The client sees these 2 changes and thinks it then needs to fire the :GetPropertyChangedSignal(), causing it to send multiple ReplaceBattery things.

(Note that I’m not 100% sure that this is the issue, but it seems likely)

That doesn’t seem to work, but I also believe it might be lag

I have fixed the issue by adding a cooldown on the ServerScript

Updated ServerScript

local replicatedStorage = game:GetService("ReplicatedStorage")

local events:Folder = replicatedStorage:WaitForChild("Events")
local useBattery:RemoteEvent = events:WaitForChild("UseBattery")
local cooldown:boolean = false

useBattery.OnServerEvent:Connect(function(player:Player, batteries:IntValue, amount:number)
	if not cooldown then
		cooldown = true
		batteries.Value = math.clamp(batteries.Value + amount, 0, 5)
		wait(1)
		cooldown = false
	end
end)

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