Int value being glitched (what I think)

This is my first time using an int value and scripting for it, and their is slight problems I think is happening.

This is the Temperature script that makes the int value that starts on 60 and cap ends on 120. It goes up every 3 seconds:

local replicatedStorage = game:GetService(“ReplicatedStorage”)

while true do
local intValue = replicatedStorage:WaitForChild(“Number”)
local remote = replicatedStorage:WaitForChild(“ListenGettingLowered”)

intValue.Value = 60

local isBeingLowered = false

remote.OnServerEvent:Connect(function(player, isLowering)
	isBeingLowered = isLowering
end)

while true do
	task.wait(3) -- Wait for 3 seconds
	if not isBeingLowered and intValue.Value < 120 then
		intValue.Value = intValue.Value + 1
		-- Ensure it doesn't exceed the cap of 120
		if intValue.Value > 120 then
			intValue.Value = 120
		end
	end
end

end

And this is the Lowering script that lowers the temperature back to 60:

local userInputService = game:GetService(“UserInputService”)
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local players = game:GetService(“Players”)

local intValue = replicatedStorage:WaitForChild(“Number”)
local listen = replicatedStorage:WaitForChild(“ListenGettingLowered”)

local decreaseAmount = 1 – Amount to decrease per interval
local decreaseInterval = 0.1 – How often to decrease the value in seconds
local minValue = 60 – Minimum value for IntValue
local maxValue = 120 – Cap value at which the player dies

local isHoldingKey = false

local function onKeyPressed(input)
if input.KeyCode == Enum.KeyCode.H then
isHoldingKey = true
– Fire remote event to notify the main script
listen:FireServer(true)
end
end

local function onKeyReleased(input)
if input.KeyCode == Enum.KeyCode.H then
isHoldingKey = false
– Fire remote event to notify the main script
listen:FireServer(false)
end
end

local function killPlayer()
local player = players.LocalPlayer
if player and player.Character then
local humanoid = player.Character:FindFirstChildOfClass(“Humanoid”)
if humanoid then
humanoid.Health = 0
end
end
end

userInputService.InputBegan:Connect(onKeyPressed)
userInputService.InputEnded:Connect(onKeyReleased)

while true do
wait(decreaseInterval)
if isHoldingKey then
– Decrease value but ensure it does not go below minValue
intValue.Value = math.max(intValue.Value - decreaseAmount, minValue)

	if intValue.Value >= maxValue then
		killPlayer()
	end
end

end
end

Whenever I lower the int value back to 60, it goes back all the way up to a higher number except for restarting itself back to 60

it was on 65, and I lowered it, but goes to 66 instead of 61

How do I fix this problem by any chance?

When making a devforum post, please ensure you put ``` on the lines before and after your code, so that it is easier to read. This will help you get faster responses!


Looking at the video, it seems to be a disconnect between the server and the player causing the ‘glitching’ back and forwards - the server doesn’t see that the client has reduced the value, so it lowers the value from what it last knew it as.

You will either need to move the code that makes the temperature go up every few seconds to the client, OR you could use a remote event to tell the server to update the temperature and to set it to the temperature that the client decided, so that what the server has matches up with what the client has.

MainTemperature is in serverscriptservice and Lowering script is in StarterPlayerScripts

Ignore the “end” mistake. I forgot to get rid of that before recording