Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is the Player not registering when Hit?

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

0

is this a localscript or a server script

0

Server Script.

1

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?

0

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.

0

Jorito was absolutely correct, I needed to reduce the wait(2) to just a wait()

1 answer

✓ Accepted answer

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!

1 comment

0

Absolutely was correct!! Thank you so very much!! :) I was overlooking this for hours!

This discussion is preserved as it was. New answers, comments, and votes are closed.