Hello !! I’m currently editing a script so I can make it better and optimized. The thing is there is this part that has to track player movement, so to do this I used a while loop to track if player moves, and if it moves it fires a function that will move the part to the player’s position. But when using loops it’s not very accurate because a loop has to wait certain time before executing again. Is there another way to make it more accurate or an alternative to loops ? Or should I just use welds?
We can’t give you good advice on how to optimize your code if you don’t give us your code, especially because loops have a purpose, and you haven’t really explained clearly what yours is doing.
RunService is the best way to do that. You can use the .Heartbeat to event to bind a function to every game frame. This allows you to run constant checks, such as tracking the player position.
Example code:
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
print("A frame has passed")
end)
Sadly you cannot use the .Changed event from parts to get player movement, since it doesn’t fire when values are changed by the physics engine, such as part movement.
Sorry I didn’t explain myself very well, I wanted to find an alternative for loops in general, I know how to optimize my game, I was just sharing some context, but .Heartbeat func solved the problem, Thanks !!
I had heard of that service and Heartbeat also, but I didn’t remember it !!, Thanks a lot !