Changing Integer Value While Using a Heartbeat:connect Function

I have a spinning object that I want to change rotational speed during gameplay. I have an integer value that I will change during gameplay after a character touches a certain part. I want my rotation script to use this new integer value as soon as this value is changed.

local spinSpeed = script.Parent.spinSpeed

game:GetService("RunService").Heartbeat:connect(function(step)
		
		script.Parent.C0 = script.Parent.C0 * CFrame.Angles(0, step * spinSpeed.Value, 0)

end)

The issue is when I change this value during gameplay, the speed of the spin does not change. However if I change the value prior to running the project, the speed does change. What I don’t understand is why on each step, the integer value isn’t being checked?

I searched around the dev hub and managed to find one possible solution involving adding a while true loop inside the function but this just froze studio after trying to test it and I don’t think it’s best practice to have these infinite loops running.

If anyone has some suggestions, I’d be really grateful!

Most probably the value is not being changed due to the server to client barrier, make sure to switch modes using studio to check the perspective on both sides.

How do you switch modes? I searched online and all I can find are articles describing the Client Server Model Roblox uses.

While playing, in the Home and Test tab there will be a button that says “Current: Client” and click on it to switch to the server and back.
If it is a problem with client changes not replicating to the server, you’ll need to use a Remote event.

Try this:

local spinSpeed = script.Parent.spinSpeed

local function updateSpinSpeed()
	game:GetService("RunService").Heartbeat:Connect(function(step)
		script.Parent.C0 = script.Parent.C0 * CFrame.Angles(0, step * spinSpeed.Value, 0)
	end)
end

updateSpinSpeed()
=
spinSpeed:GetPropertyChangedSignal("Value"):Connect(function()
	updateSpinSpeed()
end)

I gave this a shot but when I change the value of spinSpeed, the speed still remains the same.
image
image

In the above I ran the [projectd and then changed the spin speed from 1 to 10 and nothing changed in the gameplay. No error messages either.

EDIT: I removed the “=” sign (assumed it was a typo)

Just to confirm, where did you put the code I gave you?

Nevertheless, this could work:

local spinSpeed = script.Parent.spinSpeed

local function updateSpinSpeed()
	local lastSpinSpeed = spinSpeed.Value
	
	game:GetService("RunService").Heartbeat:Connect(function(step)
		if spinSpeed.Value ~= lastSpinSpeed then
			lastSpinSpeed = spinSpeed.Value
		end

		script.Parent.C0 = script.Parent.C0 * CFrame.Angles(0, step * lastSpinSpeed, 0)
	end)
end

updateSpinSpeed()