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
--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
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:
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.
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