Well, basically, I've been working on making multi-point-move-to-mini-cutscenes (I might change cutscenes to movies later on...). As of now, I've got the general script, but have been having errors letting the script know when to initiate the next MoveTo
line. The player will move to the general location, but will not stop, and keeps on moving forward. Here's what I have (of the important code):
local lplayer = game.Players.LocalPlayer.Character.Humanoid lplayer:MoveTo(Vector3.new(-184.3, 751.101, 1.9)) repeat wait() until game.Players.LocalPlayer.Character.Torso.Position.x == Vector3.new(-184.3, 751.101, 1.9) lplayer:MoveTo(Vector3.new(-169.5, 751.101, 0.9)) repeat wait() until game.Players.LocalPlayer.Character.Torso.Position == Vector3.new(-169.5, 751.101, 0.9)
This, however, is not working. I've tracked the numbers, and they change a bit every time based upon the starting position, player's lag, and in some instances, speed. Therefore, the torso's CFrame will not always reach the designated point.
I've tried using math.floor
and then checking for the refined number, however, to no avail:
local torx = game.Players.LocalPlayer.Character.Torso.Position.x local tory = game.Players.LocalPlayer.Character.Torso.Position.y local torz = game.Players.LocalPlayer.Character.Torso.Position.z local torsx = (math.floor(torx+0.5)) local torsy = (math.floor(torx+0.5)) local torsz = (math.floor(torx+0.5))
And then, on the repeat, checking like so:
repeat wait() until (torsx == -184) and (torsy == 751) and (torsz == 2)
However, whenever I print(torsx)
for instance (after the first MoveTo
line, or in the first wait()
), it returns the same value over and over.
EDIT -- As I've found out, the values that it returns are the point when the MoveTo is first initiated, even if the player has moved. Would this be attributed to me removing the player's controls?
So, my question is this; how do I fix this math.floor
method, or what is a better method to use instead of it?
Thank you for reading, and I hope to find a solution!
The issue is in that variables don't automatically update. Doing local torsx = game.Players.LocalPlayer.Character.Torso.Position.x
means 'set torsx to the torso's x at this moment', and from then on, you never update it. Try moving the variable declarations into the repeat loop, like this:
repeat local torx = game.Players.LocalPlayer.Character.Torso.Position.x local tory = game.Players.LocalPlayer.Character.Torso.Position.y local torz = game.Players.LocalPlayer.Character.Torso.Position.z local torsx = (math.floor(torx+0.5)) local torsy = (math.floor(torx+0.5)) local torsz = (math.floor(torx+0.5)) until (torsx == -184) and (torsy == 751) and (torsz == 2)
Notice that even though those are local, repeat loops can access the local variables of itself in the condition.
However! There's already a solution that would work better than what you have, built into ROBLOX. It's called "magnitude". You can use it to get the distance between two points, so what you should be doing is checking if the distance between the character's torso and where you want them is small enough:
repeat wait() until (game.Players.LocalPlayer.Character.Torso.Position-Vector3.new(-184.3, 751.101, 1.9)).magnitude<1
That'll check the distance between the point you want and the character's current location.
One more thing: since you're probably going to be using the character a lot, and the loop will be run a lot, you should set the character and the end point to a local variable. This will make your code look nicer and run faster.
local char=game.Players.LocalPlayer.Character local endPoint=Vector3.new(-184.3, 751.101, 1.9) repeat wait() until (char.Torso.Position-endPoint).magnitude<1
Finally, running that code every step is probably not important. You can set the number inside of wait to something low, but higher than the default (which is 0.03). Try setting it to 0.1 and seeing if the results still look good.