it shoots to towards the direction to which the root is looking at but i want it got point towards the players HumanoidRootPart
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")
local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart
local targetDistance = script:GetAttribute("TargetDistance")
local stopDistance = script:GetAttribute("StopDistance")
local damage = script:GetAttribute("Damage")
local attackDistance = script:GetAttribute("AttackDistance")
local attackWait = script:GetAttribute("AttackWait")
local lastAttack = tick()
local staff = humanoid.Parent.Staff.Fire
local anim = humanoid:LoadAnimation(script.Attack)
function findNearestPlayer()
local playerList = Players:GetPlayers()
local nearestPlayer = nil
local distance = nil
local direction = nil
for _, player in pairs(playerList) do
local character = player.Character
if character then
local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
if not nearestPlayer then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
elseif distanceVector.Magnitude < distance then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
end
end
end
return nearestPlayer, distance, direction
end
RunService.Heartbeat:Connect(function()
local nearestPlayer, distance, direction = findNearestPlayer()
if nearestPlayer then
if distance <= attackDistance and tick() - lastAttack >= attackWait then
lastAttack = tick()
local targetCFrame = CFrame.lookAt(root.Position, Vector3.new(nearestPlayer.Character.HumanoidRootPart.Position.X, root.Position.Y, nearestPlayer.Character.HumanoidRootPart.Position.Z))
root.Parent:PivotTo(targetCFrame)
local fireball = Instance.new("Part")
fireball.Shape = "Ball"
fireball.Size = Vector3.new(1,1,1)
fireball.Color = Color3.fromRGB(255, 94, 0)
local fire = Instance.new("Fire")
fire.Parent = fireball
fire.Heat = 0
fire.Size = 5
fire.TimeScale = 1
fire.Color = Color3.fromRGB(255, 102, 0)
fire.SecondaryColor = Color3.fromRGB(255,0,0)
fireball.Parent = workspace.Projectiles
fireball:SetNetworkOwner(nil)
fireball.CFrame = staff.CFrame
fireball.CanTouch = true
fireball.CanCollide = false
local move = Instance.new("BodyVelocity")
move.Parent = fireball
move.maxForce = Vector3.new(math.huge,math.huge,math.huge)
move.velocity = root.CFrame.lookVector*40
anim:Play()
debris:AddItem(fireball, 1)
fireball.Touched:Connect(function(hit)
if hit:IsDescendantOf(nearestPlayer.Character) and anim.IsPlaying == true and nearestPlayer.Character:FindFirstChild("Hunmanoid") then
fireball:Destroy()
nearestPlayer.Character.Humanoid.Health -= damage
end
end)
end
end
end)