So I’m currently trying to make a Drone that attack enemies. What will it do is basically if it found an enemies it will tween to enemy head but a bit higher (Vector3.new(0.1,5,0). And the only issues im having here is that the drone is not floating AT ALL and the code seems to be very buggy. Is there a way i could do this?
Code currently that i have:
local ts = game:GetService("TweenService")
local function findtarget()
local agrodistance = math.huge
local target = nil
for i, v in pairs(game.Workspace.Enemies:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso then
if (goal.Position - torso.Position).magnitude < agrodistance then
agrodistance = (goal.Position - torso.Position).magnitude
target = torso
end
end
end
return target
end
while task.wait() do
local torso = findtarget()
if torso then
for i,v in pairs(unit:GetChildren()) do
if v:IsA("MeshPart") or v:IsA("Part") then
ts:Create(v,TweenInfo.new(unitStats.Speed / 2),{Position = torso.Parent.Head.Position + Vector3.new(0,1.5,0)}):Play()
end
end
if (unittorso.Position - torso.Position).magnitude < unitRange and torso.Parent.Humanoid.Health ~= 0 then
local humanoid = torso.Parent:WaitForChild("Humanoid")
local enemystats = require(torso.Parent.Stats)
if enemystats.BulletDefense then
pcall(function() torso.Parent.Humanoid:TakeDamage(unitStats.Damage * enemystats.BulletDefense) end)
else
pcall(function() torso.Parent.Humanoid:TakeDamage(unitStats.Damage) end)
end
task.wait(unitStats.Firerate)
end
end
end
So either make it anchored or use humanoid to move it, which would be more preferable since ya can use pathfinding like SimplePath so it wouldn’t just go through walls and such
The anchored make the drone Main part tweened well but the children aren’t. I replaced the tween with
ts:Create(unittorso,TweenInfo.new(unitStats.Speed / 2),{CFrame = torso.Parent.Head.CFrame}):Play()
but the children doesn’t move and im using weld constraint
You can create a new Vector3Value to tween and then just listen to it’s .Changed event and use :PivotTo on the drone model to move it that way, so all of the children get dragged too
local ts = game:GetService("TweenService")
local model = --model
local goals = {Position = model.PrimaryPart.Position + Vestor3.new(0,5,0)}
local ti = TweenInfo.new(3)
local t = ts:Create(model.PrimaryPart, ti, goals)
t:Play()
Try doing with CFrame, i did it in my door model, it works, here i did Position, cause CFrame and adding vector 3 value confuses me, so you could try doing that.