✓ Accepted answer
We worked it out on discord.
Here's the code if anyone wants to try it out for themselves:
I tried to add comments to explain, but comment if you have any more questions.
local effects = game.ReplicatedStorage.Effects
local NPC = script.Parent
function useFireball(playerToAttack)
local fireBall = effects.MagicSkill:Clone()
fireBall.Position = NPC["Right Arm"].Position
fireBall.Parent = workspace
local weld = Instance.new("WeldConstraint", fireBall)
weld.Part0 = fireBall
weld.Part1 = NPC.RightArm
wait(0.1)
weld:Destroy()
local velocity = Instance.new("BodyVelocity", fireBall)
velocity.Velocity = CFrame.new(fireBall.Position, playerToAttack.Character.Head.Position).LookVector * 100
fireBall.Touched:Connect(function(hit)
local explosion = effects.Explosion:Clone()
if hit:IsDescendantOf(NPC)or hit:FindFirstChild("ParticleEmitter") then return end
explosion.Position = fireBall.Position
fireBall:Destroy()
explosion.Parent = workspace
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(30)
end
wait(5)
explosion:Destroy()
end)
end
while true do
wait()
for i, player in pairs(game.Players:GetChildren())do
if player.Character then
local distanceFromNPC = (NPC.Torso.Position - player.Character.UpperTorso.Position).Magnitude
if distanceFromNPC <= 100 then
useFireball(player)
wait(5)
end
end
end
end
6 comments
you can use magnitude to see how close two characters are from each other (NPC.Torso.Position - YourCharacter.Torso.Position).Magnitude
If the two characters are close in distance have the npc attack the character
You can then let the npc use the attack on the character if the character is close enough to the character.
Thanks! Also, would that be local script or script?
Also, if you don't mind, i'm still new to scripting (2-3 months of experience), could you go into detail?
Sure, do you want to talk on discord? Lucy#0003