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
I removed the wait(2)
before and after the Touched event and the script worked properly. Try removing these and let me know if this fixed the issue.
Another issue is the fireball may be too big or may be hitting another part by accident before touching the player. Because you have the fireball removed when it touches anything, it may not ever actually make it to the player for you.
If this helped, upvote/mark it as correct! Thanks!