Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can lerped positions be set directly?

This script is designed to lerp the position of a part between two target locations; where plat is the part, pos1 is the first position, pos2 is the second position, dur is the duration of the movement from one location to the next and back, and NUMBER_OF_BETWEENS is the amount of positions shown in between the target positions.

local plat=script.Parent--Part 
local pos1=plat.Value1.Value--Target Position 1
local pos2=plat.Value2.Value--Target Position 2
local dur=plat.Dur.Value--Total duration of one loop
plat.Position=pos1--Set the part position to the first value
local NUMBER_OF_BETWEENS=100--Amount of "in between" positions

while true do
	wait(dur/(NUMBER_OF_BETWEENS*2))--Wait for an amount that goes with the duration
	for i=0,1,(1/NUMBER_OF_BETWEENS) do--loop i from 0 to 1
		plat.Position=pos1:Lerp(pos2,i)--This SHOULD set the part position to the lerped amount
	end
	for i=0,1,(1/NUMBER_OF_BETWEENS) do
		plat.Position=pos2:Lerp(pos1,i)--same as before but the opposite direction
	end
end

When the script is run, the part is set to the first value, and the loop runs constantly, BUT, the part position is not updated as the loop runs. I do not see any bugs, but I am wondering if I am missing some concept.

Is there a minimum wait() duration? I don't know whether this would affect it but that's why I'm asking

Can the part position not be set to the lerped position?

Thank you for any responses! :)

1 answer

✓ Accepted answer

You don't have a wait() inside your for loops.

I added a wait() inside of your two for loops, so you can actualy see it (the part 'plat') lerp instead of being back at the starting position instantly after every loop.

Also i removed your wait(dur/(NUMBER_OF_BETWEENS*2)) because that would just delay your loop, and i suppose you don't actualy want that.

Result:

while true do
    for i=0,1,(1/NUMBER_OF_BETWEENS) do
		wait()
        plat.Position=pos1:Lerp(pos2,i)
    end
    for i=0,1,(1/NUMBER_OF_BETWEENS) do
		wait()
        plat.Position=pos2:Lerp(pos1,i)
    end
end

3 comments

0

This makes sense, I'll try it out. I think I just had the wait in the wrong place. The wait in each loop would be the dur/NUMBER_OF_BETWEENS*2 so that it loops took a controllable amount of time to execute rather than executing as fast as possible.

0

It worked. Thanks!

0

No problem! Glad i could help you :D

This discussion is preserved as it was. New answers, comments, and votes are closed.