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)