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

how do I make a part (for example a coin) move left and right ?

how do I make a part (for example a coin) move left and right coordinately (first to one side, then it comes back and repeats the process)? I created a script, but the part (coin) does not move correctly. script:

coroutine.resume(coroutine.create(function()
	
	while true do
	wait(0.1)
	local v = script.Parent.Position
	for i = 1,30 do
		wait(0.1)
		script.Parent.Position = script.Parent.Position + Vector3.new(i/40,0,0)
	print('top1')
	end
	for i = 1,30 do
		wait(0.1)
		script.Parent.Position = script.Parent.Position - Vector3.new(i/40,0,0)
	print('top2')
	end
	end
	end))

1 answer

✓ Accepted answer

What I would do is use TweenService. It is very smooth and neat.

I would also recommend editing the CFrame of the part instead of the position, so you do not run into collision problems, as CFrame ignores collision.

Here is an example of how you can use it.

local TweenService = game:GetService("TweenService")

local partToMove = script.Parent
local originalCFrame = partToMove.CFrame

local tweenInfo = TweenInfo.new(
    2, -- Time it takes to go from position to another
    Enum.EasingStyle.Linear, -- EasingStyle
    Enum.EasingDirection.Out, -- EasingDirection
    -1, -- RepeatCount (when less than zero the tween will loop indefinitely)
    true, -- Reverses (tween will reverse once reaching it's goal)
    0 -- DelayTime
) 

local goal = {
    CFrame = originalCFrame * CFrame.new(3, 0, 0) -- move by 3 studs to right -- mistake was here
}

local tween = TweenService:Create(partToMove, tweenInfo, goal)
tween:Play()

That should do it, hope I helped.

If you have further questions please let me know!

4 comments

0

hey bro, thanks for the script, i copied it and put it here, but it didn't work, the part (coin) was stopped. know what it can be? I also fixed some digit errors, like 'ParToMove' instead of 'PartToMove', which you wrote.

0

I apologize for that I wrote out the script very quick and wasn't on an editing software so I missed a few mistakes, I have updated my answer

0

it actually worked! it looks like my Studio was in trouble. Thanks, really!

0

I'm sorry for saying that it didn't work

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