Help with flashbang

I combined a grenade script I already had with a sight detection RayCast script and it’s giving me this error:

Error
 Workspace.Handle.Damage:28: attempt to index nil with 'Head'  -  Server - Damage:28
  13:39:04.694  Stack Begin  -  Studio
  13:39:04.694  Script 'Workspace.Handle.Damage', Line 28 - function Explode  -  Studio - Damage:28
  13:39:04.694  Script 'Workspace.Handle.Damage', Line 71 - function boom  -  Studio - Damage:71
  13:39:04.694  Script 'Workspace.Handle.Damage', Line 75  -  Studio - Damage:75
  13:39:04.694  Stack End  -  Studio
Damage Script
local Object = script.Parent
local Damage = script:WaitForChild("Damage").Value
local Radius = script:WaitForChild("Radius").Value
local FuseTime = script:WaitForChild("FuseTime").Value
local Tag = Object:WaitForChild("creator")
local Debris = game:GetService("Debris")

local Angle = 0.7


function tagHuman(human)
	local tag = Tag:Clone()
	tag.Parent = human
	game:GetService("Debris"):AddItem(tag)
end

function OnExplosionHit(Character)
	local Humanoid = Character:FindFirstChild("Humanoid")
	
	if Humanoid and Humanoid.Health > 0 then
		tagHuman(Humanoid)
	    Humanoid:TakeDamage(Damage)
	end
end

function Explode(Character)
	
	local unit = ((Object.Position - Character.Head.Position) * Vector3.new(1,0,1)).Unit
	local HeadLookVec = Character.Head.CFrame.LookVector * Vector3.new(1,0,1)
	local IsFacing = HeadLookVec:Dot(unit) > Angle
	
	local Explosion = Instance.new("Explosion")
	--Explosion.Visible = false
	Explosion.BlastRadius = Radius
	Explosion.BlastPressure = 0
	
	local Light = script.Parent.Parent.Handle.Light
	
	Explosion.Position = Object.Position
	Explosion.Parent = Object
	
	Light.Enabled = true
		
	Explosion.Hit:Connect(function(hit)		
		if hit.Name == "HumanoidRootPart" and hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("RagdollHumanoid") then
			
			OnExplosionHit(hit.Parent)		
			
			end	
	end)
	
	if IsFacing then
		
		print("Looked")
		
		Light:Destroy()
		
	end	
		
	wait(2.5)
	Object:Destroy()
	
end

function boom()
	wait(FuseTime)
	Object.Anchored = true
	Object.CanCollide = false
	Object.Transparency = 1
	Object.Explode:Play()
	Explode()
end

spawn(function()
	boom()
end)

How can I fix this?

1 Like

Can you add a comment next to the line that errored?

1 Like

This is the line that was causing the error.

1 Like

The Explode function has a character parameter however you did not provide a character when calling the function.

2 Likes

This is what I thought might be the problem.

I was gonna say that lol but yeah

Got it working. It took a while to find a good way to define character. Do you think this will cause any problems?

function boom()

	for _, Character in ipairs(game.Workspace:GetChildren()) do
		if Character:IsA("Model") and Character:FindFirstChild("Humanoid") then

			wait(FuseTime)
			Object.Anchored = true
			Object.CanCollide = false
			Object.Transparency = 1
			Object.Explode:Play()
			Explode(Character)

		end
	end
end

This looks like it would create a new explosion for every character in the game which probably isn’t intended.

I think a better way to go about it is to revert this change and remove the Character parameter from the Explosion function and move all of the IsFacing logic to inside the OnExplosionHit function so that it will only check if a character is facing it if they were hit by the explosion.

I tested it with multiple people and it seems to work as expected. I wouldn’t want it to be if they were hit by the explosion as that damages them and that should only be hitting them if they are on the bomb during the explosion.

Oh I’ve just noticed that it destroys the Object at the end of the Explode function which also destroys the script so it will only explode once which means the IsFacing logic would only work on the first character in the workspace.

That is true. How could I fix this?

I would probably loop through the characters within the explode function instead of outside of it and then perform the IsFacing logic within that loop and also probably add a distance check to make sure they are close enough and then a raycast from the flashbang to the character to make sure there aren’t any walls in between.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.