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.
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.
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.
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()