I made this script where the unit flies toward the target and punches them and then return back to their original position after the attack. But then this happens… They start sliding out of the place holder. I would like to know the reason on why this happenss… Thank youuu…
Here is the script, I use tween service
function UnitSpawnModule.Attack(newUnit, player)
local Config = newUnit:WaitForChild("Config")
local target = UnitSpawnModule.FindTarget(newUnit, Config.Range.Value, Config.TargetMode.Value)
if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
if newUnit.Config.TowerName.Value == "Mordecus" then
local targetcframe = CFrame.lookAt(newUnit.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newUnit.HumanoidRootPart.BodyGyro.CFrame = targetcframe
local MobsHit = {}
local TargetPlaceHolder = Instance.new("Part")
local humanoidRootPart = target.HumanoidRootPart
if humanoidRootPart and target.Humanoid then
if not humanoidRootPart and not target.Humanoid then
return
end
TargetPlaceHolder.Size = target.HumanoidRootPart.Size
TargetPlaceHolder.Position = target.Head.Position
TargetPlaceHolder.Name = "Target"
TargetPlaceHolder.Parent = workspace.Misc
TargetPlaceHolder.Anchored = true
TargetPlaceHolder.CanCollide = false
TargetPlaceHolder.CanQuery = false
TargetPlaceHolder.Transparency = 0
wait(.3)
AnimateUnitEvent:FireAllClients(newUnit, "Attack1", target)
newUnit.Head.Punch:Play()
TweenService:Create(newUnit.PrimaryPart, TweenInfo.new(0.6),{Position = TargetPlaceHolder.Position}):Play()
local DamagePart = Instance.new("Part")
DamagePart.Size = Vector3.new(4.5,1,4.5)
DamagePart.Transparency = 1
DamagePart.Color = Color3.new(1, 0.329412, 0.329412)
DamagePart.Position = TargetPlaceHolder.Position
DamagePart.Name = "Target"
DamagePart.Parent = workspace.Misc
DamagePart.Anchored = true
DamagePart.CanCollide = false
DamagePart.CanQuery = false
local debounce = false
DamagePart.Touched:Connect(function(hit)
if hit.Parent.Parent == workspace.Mobs then
if table.find(MobsHit, hit.Parent) then
return
end
hit.Parent.Humanoid:TakeDamage(Config.Damage.Value)
player.leaderstats["Total Damage"].Value += Config.Damage.Value
table.insert(MobsHit, hit.Parent)
delay(1, function()
local index = table.find(MobsHit, hit.Parent)
table.remove(MobsHit,index)
end)
DamagePart:Destroy()
end
end)
if target.Humanoid.Health <= 0 then
player.Money.Value += target.Money.Value
player.Kills.Value +=1
end
end
wait(1.6)
TweenService:Create(newUnit.PrimaryPart, TweenInfo.new(.15),{Position = newUnit.PlaceHolder.Position}):Play()
task.wait(Config.Cooldown.Value)
TargetPlaceHolder:Destroy()
end
end
task.wait(.1)
if newUnit and newUnit.Parent then
UnitSpawnModule.Attack(newUnit, player)
end
end
store the original cframe of the unit before it moves
function UnitSpawnModule.Attack(newUnit, player)
local Config = newUnit:WaitForChild("Config")
local target = UnitSpawnModule.FindTarget(newUnit, Config.Range.Value, Config.TargetMode.Value)
if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
if newUnit.Config.TowerName.Value == "Mordecus" then
local originalCFrame = newUnit.HumanoidRootPart.CFrame
local targetcframe = CFrame.lookAt(newUnit.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newUnit.HumanoidRootPart.BodyGyro.CFrame = targetcframe
local MobsHit = {}
local TargetPlaceHolder = Instance.new("Part")
local humanoidRootPart = target.HumanoidRootPart
if humanoidRootPart and target.Humanoid then
if not humanoidRootPart and not target.Humanoid then
return
end
TargetPlaceHolder.Size = target.HumanoidRootPart.Size
TargetPlaceHolder.CFrame = target.Head.CFrame
TargetPlaceHolder.Name = "Target"
TargetPlaceHolder.Parent = workspace.Misc
TargetPlaceHolder.Anchored = true
TargetPlaceHolder.CanCollide = false
TargetPlaceHolder.CanQuery = false
TargetPlaceHolder.Transparency = 0
wait(.3)
AnimateUnitEvent:FireAllClients(newUnit, "Attack1", target)
newUnit.Head.Punch:Play()
TweenService:Create(newUnit.PrimaryPart, TweenInfo.new(0.6), {CFrame = TargetPlaceHolder.CFrame}):Play()
local DamagePart = Instance.new("Part")
DamagePart.Size = Vector3.new(4.5,1,4.5)
DamagePart.Transparency = 1
DamagePart.Color = Color3.new(1, 0.329412, 0.329412)
DamagePart.CFrame = TargetPlaceHolder.CFrame
DamagePart.Name = "Target"
DamagePart.Parent = workspace.Misc
DamagePart.Anchored = true
DamagePart.CanCollide = false
DamagePart.CanQuery = false
local debounce = false
DamagePart.Touched:Connect(function(hit)
if hit.Parent.Parent == workspace.Mobs then
if table.find(MobsHit, hit.Parent) then
return
end
hit.Parent.Humanoid:TakeDamage(Config.Damage.Value)
player.leaderstats["Total Damage"].Value += Config.Damage.Value
table.insert(MobsHit, hit.Parent)
delay(1, function()
local index = table.find(MobsHit, hit.Parent)
table.remove(MobsHit,index)
end)
DamagePart:Destroy()
end
end)
if target.Humanoid.Health <= 0 then
player.Money.Value += target.Money.Value
player.Kills.Value +=1
end
end
wait(1.6)
TweenService:Create(newUnit.PrimaryPart, TweenInfo.new(.15), {CFrame = originalCFrame}):Play()
task.wait(Config.Cooldown.Value)
TargetPlaceHolder:Destroy()
end
end
task.wait(.1)
if newUnit and newUnit.Parent then
UnitSpawnModule.Attack(newUnit, player)
end
end
function UnitSpawnModule.Attack(newUnit, player)
local Config = newUnit:WaitForChild("Config")
local target = UnitSpawnModule.FindTarget(newUnit, Config.Range.Value, Config.TargetMode.Value)
if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
if newUnit.Config.TowerName.Value == "Mordecus" then
local placeHolderCFrame = newUnit.PlaceHolder.CFrame
local targetcframe = CFrame.lookAt(newUnit.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newUnit.HumanoidRootPart.BodyGyro.CFrame = targetcframe
local MobsHit = {}
local TargetPlaceHolder = Instance.new("Part")
local humanoidRootPart = target.HumanoidRootPart
if humanoidRootPart and target.Humanoid then
if not humanoidRootPart and not target.Humanoid then
return
end
TargetPlaceHolder.Size = target.HumanoidRootPart.Size
TargetPlaceHolder.CFrame = target.Head.CFrame
TargetPlaceHolder.Name = "Target"
TargetPlaceHolder.Parent = workspace.Misc
TargetPlaceHolder.Anchored = true
TargetPlaceHolder.CanCollide = false
TargetPlaceHolder.CanQuery = false
TargetPlaceHolder.Transparency = 0
wait(.3)
AnimateUnitEvent:FireAllClients(newUnit, "Attack1", target)
newUnit.Head.Punch:Play()
TweenService:Create(newUnit.PrimaryPart, TweenInfo.new(0.6), {CFrame = TargetPlaceHolder.CFrame}):Play()
local DamagePart = Instance.new("Part")
DamagePart.Size = Vector3.new(4.5,1,4.5)
DamagePart.Transparency = 1
DamagePart.Color = Color3.new(1, 0.329412, 0.329412)
DamagePart.CFrame = TargetPlaceHolder.CFrame
DamagePart.Name = "Target"
DamagePart.Parent = workspace.Misc
DamagePart.Anchored = true
DamagePart.CanCollide = false
DamagePart.CanQuery = false
local debounce = false
DamagePart.Touched:Connect(function(hit)
if hit.Parent.Parent == workspace.Mobs then
if table.find(MobsHit, hit.Parent) then
return
end
hit.Parent.Humanoid:TakeDamage(Config.Damage.Value)
player.leaderstats["Total Damage"].Value += Config.Damage.Value
table.insert(MobsHit, hit.Parent)
delay(1, function()
local index = table.find(MobsHit, hit.Parent)
table.remove(MobsHit,index)
end)
DamagePart:Destroy()
end
end)
if target.Humanoid.Health <= 0 then
player.Money.Value += target.Money.Value
player.Kills.Value +=1
end
end
wait(1.6)
local returnTween = TweenService:Create(newUnit.PrimaryPart, TweenInfo.new(.15), {CFrame = placeHolderCFrame})
returnTween:Play()
returnTween.Completed:Wait()
newUnit.HumanoidRootPart.Anchored = true
newUnit.HumanoidRootPart.CFrame = placeHolderCFrame
task.wait()
newUnit.HumanoidRootPart.Anchored = false
task.wait(Config.Cooldown.Value)
TargetPlaceHolder:Destroy()
end
end
task.wait(.1)
if newUnit and newUnit.Parent then
UnitSpawnModule.Attack(newUnit, player)
end
end
I’m pretty sure this has more to do with the rig than the code. Was it custom made in blender or is it just parts? Edit: Couldn’t you just anchor the Root?
The rig is made in blender and is supposed to be r6, I just deleted the legs and created the Feet parts in blender. Then added 3 joints for the ankles, knees and the upper leg… Should I use a normal rig??