Hello!
Adapted a Walking NPC/Fireball shooter script from DutchDeveloper. Its a Script placed inside an NPC, the NPC walks around, shoots fireballs. Only one slight problem - it does not register when it Hits a Player. Line 64-69 is the relevant parts, but I included the entire script just in case.
No errors, when i put some Prints in to show whats being hit, it'll read back "baseplate" or "wall" or whatever else it hits, but never a Player, or anything with a Humanoid in it.
Thoughts? Advice? Many thanks in advance!
Script:
local Humanoid = script.Parent:WaitForChild("Humanoid")
local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local WalkRange = 20
local FireballSpeed = 100
local Damage = 25
local Closest = nil
spawn(function()
while true do
wait()
if Closest ~= nil then
HumanoidRootPart.BodyGyro.CFrame = CFrame.new(HumanoidRootPart.Position, Vector3.new(Closest.Character.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, Closest.Character.HumanoidRootPart.Position.Z))
end
end
end)
while true do
wait(2)
local RandomValue = math.random(0,10)
local List = {}
for i,v in pairs(game.Players:GetChildren()) do
if v and v.Character then
local Distance = (HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude
table.insert(List, {v, Distance})
end
end
table.sort(List,function(A,B)
return A[2] <= B[2]
end)
if #List >=1 then
Closest = List[1][1]
if RandomValue > 0 and RandomValue < 6 then
local RandomWalkPosition = Vector3.new(math.random(-WalkRange,WalkRange),0, math.random(-WalkRange,WalkRange))
Humanoid:MoveTo(HumanoidRootPart.Position + RandomWalkPosition)
else
local Fireball = Instance.new("Part")
Fireball.Anchored = false
Fireball.Shape = "Ball"
Fireball.Size = Vector3.new(5,5,5)
Fireball.TopSurface = Enum.SurfaceType.Smooth
Fireball.BottomSurface = Enum.SurfaceType.Smooth
Fireball.Material = Enum.Material.Sand
Fireball.BrickColor = BrickColor.new("Institutional white")
Fireball.CFrame = HumanoidRootPart.CFrame*CFrame.new(0,0,-10)
Fireball.Parent = workspace
Fireball.CanCollide = false
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(100000,100000,100000)
local Origin = HumanoidRootPart.Position
BV.Velocity = (List[1][1].Character.HumanoidRootPart.Position - Origin).unit * FireballSpeed
BV.Parent = Fireball
spawn(function()
wait(2)
Fireball.Touched:Connect(function(Hit) -- STARTS HERE
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
Hit.Parent.Humanoid:TakeDamage(Damage)
end
Fireball:Destroy()
end)
end)
wait(2)
end
end
end

5 comments
is this a localscript or a server script
Server Script.
Are you sure that the fireball is hitting the player after two seconds? You have a
wait(2)that occurs before the Touched event is connected, so anything that the Fireball hits before those 2 seconds will not register. Also, are you sure that the rest of the script (including the firing of the fireball) works as intended?Yep! and Yes. :) The script can be tested by inserting a Script with that, and adding a BodyGyro to HumanoidRootPart, of any NPC model you may have.
Jorito was absolutely correct, I needed to reduce the wait(2) to just a wait()