You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make the enemies move but my tweens wont work.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
None.
local function MoveEnemySmoothly(model, Path)
-- Path is just the position of where to send the enemy
local enemy = model:WaitForChild('HumanoidRootPart')
local humanoid = model:WaitForChild('Humanoid')
local offset = (enemy.CFrame.Position - Path.Position)
local distance = offset.Magnitude
local direction = offset.Unit
local speed = 10
local cframe = Path.CFrame
local tween = game:GetService("TweenService"):Create(
enemy,
TweenInfo.new(speed, Enum.EasingStyle.Linear),
{CFrame = cframe}
)
return tween
end
local function ChildAdded(enemy)
for i = 1, #workspace:WaitForChild('Points'):GetChildren() do
print('Moving to point!')
local foundPoint = workspace:WaitForChild('Points'):WaitForChild(tostring(i))
local createdTween = MoveEnemySmoothly(enemy, foundPoint)
local StopLoop = false
--task.spawn(function()
-- while true do
-- if StopLoop then
-- break
-- end
-- local root
-- for i,v in enemy:GetDescendants() do
-- if v.Name == 'HumanoidRootPart' then
-- root = v
-- end
-- end
-- root.CFrame = CFrame.lookAt(foundPoint.Position,root.Position)
-- task.wait()
-- end
--end)
createdTween:Play()
createdTween.Completed:Wait()
StopLoop = true
end
end
workspace:WaitForChild('Enemies').ChildAdded:Connect(function(enemy)
ChildAdded(enemy)
end)
Since you are trying to tween an enemy, sadly you can’t tween unanchored parts. You can try anchoring it before tweening and unanchoring it after it completes, or if you don’t want all this you can just use Humanoid:MoveTo()
Hey there, I love Tower Defense games and how they actually perform in Roblox. One good example of how enemies move is by looking at Tower Defense Simulator. This isn’t really the solution to your code, but I thought that it would help to give you some information about how much more simple your game can really be, structurally.
It seems like your code is doing the movement on the server side, correct me if I’m wrong. In TDS, all towers and enemies are actually stationary and don’t move anywhere on the server. If you were playing on the server side of things, it would look something like this:
So, how does TDS and other tower defense games move their models? Well, they use scripts and modules that store each model’s CFrame, Health, Speed, Abilities, and many more different properties.
With this in mind, you only need to tween the CFrame to different points of the map, correct? In this type of structure, you can simply store the destination position in the server script, and tween the model to those points on the client side with a localscript, simulating what you see in tower defense games. Once again, on the server side, this will look nothing like how it is on the client.
The reason why many established tower defense games use this type of structure is to reduce as much lag and bottleneck possible, while keeping a high amount of enemies on the map. Most of the cosmetic work is performed on the client side.
Hmm, if that’s the case, you should already know how to tween your models properly, correct? If you’re getting no errors and your enemies aren’t moving, perhaps you should look over your code, add debug statements, and double check what you’re tweening.