Hello, I’m trying to make a freezer tower in my tower defense game , but i cant figure out how to change the speed of the tween instantly cause the freezer slows the enemies down and I don’t want to wait until it reaches the next waypoint for it to update.
Right now if the tower shoots a zombie it just teleports to the next waypoint.
for waypoint = 2, #workspace.Waypoints:GetChildren() do
local previousPoint = workspace.Waypoints[waypoint - 1]
local nextPoint = workspace.Waypoints[waypoint]
enemy.HumanoidRootPart.Anchored = true
if enemy:FindFirstChild("MovingTo") then
enemy.MovingTo.Value = waypoint
end
local Distance = (previousPoint.Position-nextPoint.Position).Magnitude
--enemy.HumanoidRootPart.Orientation = nextPoint.Orientation
TS:Create(enemy.PrimaryPart, TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0), {Orientation = nextPoint.Orientation}):Play()
local TravelTime = Distance / enemy:WaitForChild("Humanoid").WalkSpeed
local MoveAnimation = TS:Create(enemy.HumanoidRootPart, TweenInfo.new(TravelTime,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0), {Position = nextPoint.Position})
MoveAnimation:Play()
enemy.Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
MoveAnimation:Cancel()
MoveAnimation = TS:Create(enemy.HumanoidRootPart, TweenInfo.new(TravelTime,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0), {Position = workspace.Waypoints[waypoint].Position})
MoveAnimation:Play()
end)
MoveAnimation.Completed:Wait()
end
if enemy:FindFirstChild("Humanoid") then
BaseModule.UpdateHealth(enemy.Humanoid.Health)
end
enemy:Destroy()
end
If you know the solution to this please let me know.
I actually had this issue aswell when making a freeze tower. So i would like to inform on how i tackled this issue. I tried to make it somehow work with using tweens but sadly couldn’t. I resorted into using the Lerp function instead which seemed to work great.
Here is the idea of how it would go:
Orientation
--Orientation / Look at part.
local CFrame1 = enemy.HumanoidRootPart.CFrame -- Cframe of enemy
local CFrame2 = CFrame.new(CFrame1.Position, nextPoint.CFrame.Position)
for i=0,1,0.1 do
enemy.HumnaoidRootPart.CFrame = CFrame1:Lerp(CFrame2,i)
wait(0.05)
end
Position
local PosDifference = nextPoint.Position - enemy.HumanoidRootPart.Position
local Distance = PosDifference.magnitude
local HumanoidCFrame = enemy.HumanoidRootPart.CFrame
local newCFrame = CFrame.new(enemy.HumanoidRootPart.Position,nextPoint.Position) + PosDifference
local speed = enemy:WaitForChild("Humanoid").WalkSpeed -- Speed of the enemy
for i=0,speed,1/Distance do
enemy.HumanoidRootPart.CFrame = HumanoidCFrame:Lerp(newCFrame,i/speed)
wait()
end
Now, there are many ways to implement the freeze feature, but this is how i did it.
Have every enemy equipped with a bool value inside of their HumanoidRootPart, For example called Frozen.
Have the Freeze tower to set the value to true when the tower fires at the enemy.
Check if the enemy’s Frozen value is true inside of the Lerp loop. If yes, then pause the loop, causing the enemy to stay still.
Now revert the Frozen value back to false, This can be done inisde of the tower script or the enemy script.
This is how it would look like:
for i=0,speed,1/Distance do
if enemy:FindFirstChild("HumanoidRootPart") then -- check if enemy still exists
local Frozen = enemy.HumanoidRootPart.Frozen -- get the frozen value
enemy.HumanoidRootPart.CFrame = HumanoidCFrame:Lerp(newCFrame,i/speed)
if Frozen.Value == true then--pause the loop until frozen is false again.
repeat wait() until Frozen.Value == false
end
end
wait()
end