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

Can I make a part move in smaller increments and maintain a faster speed through this method?

Hi,

Currently I'm using the following code to move a part as shown below per Heartbeat. There is code following this that will react based on whatever the part comes in contact as it's moving down a straight path. I have run into two issues while using this method that I'm seeking advice on how to solve.

game:GetService("RunService").Heartbeat:Connect(function()
		Part.CFrame = Part.CFrame*CFrame.new(0,-1,0)

So the first issue that I have noticed is that there are instances where if a part is in the middle of the previous CFrame location and the new CFrame location, there is no direct touch which prevents any interaction codes from being applied. Now of course, the logical thing to do is to have the part move more frequent than just 1 stud at a time, moving at .1 for example. This runs into the second issue..

The second issue is that the lower the number of movement studs, the slower the object moves along a line. In terms of, let's say a bullet traveling, instead of having that quick snap along a line it will slowly track across until interacting or being removed.

So my question here is, is there a way to make the part move in smaller increments but increase the speed at which it travels? I would prefer any answers to enhance the above method, but I am open for suggestions on different methods of manipulating the part.

Thanks!

Romul

1 comment

0

I edited the answer and commented to give some more context.

1 answer

✓ Accepted answer

I would specifically use TweenService to move the part.

Heres an edited example of using TweenService:



    local TweenService = game:GetService("TweenService") --Gets the service
     
    local part = Instance.new("Part") --Creates the part
    part.Position = Vector3.new(0, 10, 0) --Starting position
    part.Color = Color3.new(1, 0, 0) --Part color
    part.Anchored = true --Anchoring part
    part.Parent = game.Workspace --Getting the part in the workspace
     
    local goal = {} --Table of properties to tween to
    goal.Position = Vector3.new(10, 10, 0) --This will be the end position
    goal.Color = Color3.new(0, 1, 0) --This will be the end color
     
    local tweenInfo = TweenInfo.new(5) --First arg defines the time taken for the tween to change the part's properties.
     
    local tween = TweenService:Create(part, tweenInfo, goal) --Creates tween object
     
    tween:Play() --Starts tween object.

If you are wondering about the end position, its quite simple! Instead of modifying the cframe directly, use tweenservice and repeat using tweenservice after each tween ends, modifying the end position each time!

3 comments

0

I had done a little research into using TweenService, but one issue I encountered was not having an end position. The part essentially will just move along a line until certain circumstances are met. I.E. Hitting a person, object, or removing/fading.

0

Romul, simply set a small end position ahead of the object, and keep repeating doing tweens for each individual position

0

I have accepted this answer as it would correctly handle the issue I described. I did however decide to go with a different manner of handling the CFrame that allowed me to propel the object along is CFrame instead of just simply changing it. Thanks Ben for the help!

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