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 move anchored parts Smoothly?

Hello everyone.What I would like to know is: Is there some way to move Anchored Parts besides CFrame or to make CFrame movement smoother? I have been developing a bomberman game, and to move enemies on a 5 studs grid I used a CFrame loop method:

local npc = map.Npc
local walktime = 10
local distanceinstuds = 5
for i=1,walktime do
	wait()
	local pos = npc.Position+npc.CFrame.LookVector*(distanceinstuds/walktime)
	npc.CFrame = CFrame.new(pos,pos+npc.CFrame.LookVector)
end

It worked perfectly fine on roblox studio, both on Play and "Start Server", but when I went on to play the game the npc movement wasn't smooth and sometimes they took several seconds to update their position to the client. I saw that using CFrame to make movement is somewhat "Bad" and united with the wait() loop its probably the problem.

So, repeating the Question: Is there some way to move Anchored Parts besides CFrame or to make CFrame movement smoother?

1 answer

✓ Accepted answer

There is indeed a smoother way to move NPCs along a map. There are two methods to do this. The first one is the best for you.

Using a Humanoid and Humanoid:MoveTo()

I apologize if I give any errors during this explanation, as I am working on learning this myself, but I understand how it mostly works.

First of all, you'd want to insert a humanoid into the NPC. This accesses Health, MaxHealth, WalkSpeed, JumpPower, etc. It is everything you would want to create an NPC. The best way to create one is to insert a rig, then adjust that rig, but with extra hard painful work, you could make a custom rig with more than R6 or R15 functions. Ensure it has a HumanoidRootPart and everything inside the humanoid is unanchored. You do not need to weld the humanoid together, but if anything is attached to it like a sword, you will need to weld it to the body part.

Then, you want to utilize the function "MoveTo()" which will move any humanoid that is able to and unanchored to a designated location. It even plays a smooth animation that either you made or Roblox gave you! MoveTo takes a vector3 value. It could also be a part's location!

Using the TweenService.

Now normally, if you just want to smoothly move a part along, you can just use part:TweenPosition(Vector3Value). However, there is a different way that it can be done by using the Tweening Service. It's hard to explain, so I will leave an example below.

game.Players.LocalPlayer.PlayerGui:WaitForChild("Dark")
local Darkness = game.Players.LocalPlayer.PlayerGui.Dark.Ness
local TweenService = game:GetService("TweenService")
local Info1 = TweenInfo.new(
	1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out,
	0,
	false,
	1
)
local goals1 = {
	Transparency = 0
}
local Vgoals = {
	Volume = 0
}
local Info2 = TweenInfo.new(
	1,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out,
	0,
	false,
	1
)
local goals2 = {
	Transparency = 1
}
local Debounce = false

script.Parent.MouseButton1Click:Connect(function()
	if Debounce == false then
		Debounce = true
		workspace.Selection:Play()
		local Tween1 = TweenService:Create(Darkness, Info1, goals1)
		local VolTween = TweenService:Create(workspace.MainTheme, Info1, Vgoals)
		VolTween:Play()
		Tween1:Play()
		Tween1.Completed:Wait()
		workspace.MainTheme:Stop()
		wait(1)
		workspace.CurrentCamera.CFrame = workspace.CCC.CFrame
		script.Parent.Parent.Parent.Enabled = false
		game.Players.LocalPlayer.PlayerGui.CCC.Faction.Enabled = true
		workspace.CCCTheme:Play()
		local Tween2 = TweenService:Create(Darkness, Info2, goals2)
		Tween2:Play()
		Tween2.Completed:Wait()
		wait(1)
		Debounce = false
	end
end)

Like animations, the TweenService needs to be created, then played. If you need more help, the roblox developer wiki will have more information on the tweenservice and it's values.

5 comments

0

Just for his sake, you should probably simplify this example down, it has a lot of things he doesn't need, and it probably will confuse him. Also I do recommend using the TweenService over moveto for npcs because its just easier to have them move and play custom animations at the same time especially if the NPCs themselves are not humanoids.

0

Then CFrame would probably be the best thing for you. But it would be laggy depending on the situation it's being used in, or you can decrease the wait value and movement the NPC makes per loop. This will make it look smoother while using CFrame

0

I wont use MoveTo() because my NPC is anchored, which is recquired for him not to get displaced by any other reason. Also the TweenService might make the speed of the NPC stable, but it still creates excessive stress on the server.

0

Maybe I should make the NPC invisible on serverside and the cframe loop have less cycles, while I clone its model clientside without any collidableparts and make a loop where it tweens to the serverside npc position, so the stress isnt so big for the server

0

That could also work, or remove the CFrame loop entirely, do that, and then move the NPC to it's desired location after that has finished.

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