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?
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.