Rotate animation for coins starts getting faster when I run the game

  • What does the code do and what are you not satisfied with?
    I have a basic script running in the StarterCharacterScripts to animate/rotate my coins in the game. But the issue is the animation keeps getting faster and faster when you run the game.

  • What potential improvements have you considered?
    I tried looking all over devforum and also googled it but I didnt find anything that could help me

  • How (specifically) do you want to improve the code?
    I want the animation speed of the coin to stay the same

Capture

--Here is the script
local coin = script.Parent
local speed = 1

while task.wait() do
	for i = 1,360 do
		coin.Orientation = coin.Orientation + Vector3.new(0,speed,0)
		task.wait()
	end
end
1 Like

Is it because your constantly looping the for loop?

1 Like

try this
while wait() do
coin.Orientation = coin.Orientation + Vector3.new(0,speed,0)
end

Probably yes but how do I fix that

Just get rid of the for loop like 12312ababc said

Only have the while loop

Uh? Yes? Idk what you’re asking sorry!

Just put

while task.wait() do
coin.Orientation = coin.Orientation + Vector3.new(0, speed, 0)
end

See if that works!

I’m guessing that what’s happening here is that your CoinMain script is constantly cloning new scripts into the same coin which makes it constantly spin faster.

I don’t really see what else could be the issue here as I’ve tested your script just to make sure there isn’t some strange bug and your script works just fine.

Also, you do realize that scripts in StarterCharacterScripts will re-initialize every time your character respawns right? I’d consider moving all of it to StarterPlayerScripts instead.

Another thing to consider is whether or not you care about updating the coins rotation to where it is supposed to be if there’s lag. Here’s an example of what happens with your current script on rotating parts under lag:

lagconsideration-min

An accumulator loop will update the position to where it is supposed to be after lag.

Here’s the script. Also, I don’t recommend copy pasting this into every rotating coin, update them from the loop instead.

--!strict

local RS = game:GetService("RunService")

local coin: Part = workspace:FindFirstChild("Coin") or workspace:WaitForChild("Coin", 3)
local speed: number = 1
local rate: number = 1 / 60 -- 60 times a second (normalized value).
local accumulated: number = 0

RS.RenderStepped:Connect(function(deltaTime: number)
	accumulated += deltaTime
	while accumulated >= rate do
		accumulated -= rate
		coin.Orientation += Vector3.new(0, speed, 0)
	end
end)

In reality though I would advise with using Mover Constraints because it’s by far the smoothest and non-performance heaviest way to apply such rotations on multiple objects at a time.

why not move the script to server script service?

Keep in mind that task.wait doesn’t always wait the same amount of time, this could be why it seems to go faster. On top of this, I’d suggest using CollectionService as well to optimise your script.

As for task.wait not being consistent;

This script

for count = 0, 4, 1 do
	
	local timeNow = tick()
	
	task.wait()
	
	print(tick()-timeNow)
	
end

Outputs this
image

Because it’s better to run animations on clients instead of stressing out the server?

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