Help with my Blood Hit Effect and Damage Noise Script

So I wanted to make a simple blood effect and sound when you get damaged, because I removed the health bar at the top which removes the blood effect.

My code keeps giving me this error: Workspace.Babybunnyiscute19.BloodFlash:8:Attempt to index nil with ‘Connect’

Here is the code:

local plr = game.Players.LocalPlayer
local char = plr.Character
local hmoid = char:GetChildren("Humanoid")
local onScreen = false

local previousHealth = hmoid.Health

hmoid.HealthChanged:Connect(function(health)
	if health > previousHealth then
		onScreen = true
		script.HitSound:Play()
		game.StarterGui.BloodFlashGui.BloodFlashImage.Visible = true
		wait(1)
		game.StarterGui.BloodFlashGui.BloodFlashImage.Visible = false
		onScreen = false
	end
	previousHealth = health
end)

The code was in StarterCharacterScripts and I found out this function on reddit, So when I got this error, I copied the script and added my code to it, and it still didn’t work.

Please give a reason why it doesn’t work and a solution.

1 Like

The issue is the way you are getting the characters’s humanoid. You’re using GetChildren("Humanoid"), that’s not the correct use of GetChildren, try WaitForChild("Humanoid")

Technically, what happened here is that you tried to use :Connect on a non-existing event / non-existing instance (aka object).

I believe it’s obvious to notice, but what is happening here is that you used :GetChildren incorrectly (line 3). What you should use, and what you’d want to use is :GetChildren()['Humanoid']. This is because :GetChildren doesn’t take a parameter - instead, it returns a table containing the instance’s children, meaning you would use brackets to index something (in this case, since it is a dictionary, you use strings to index stuff, but with a array, you’ll use numbers / indexes).

The text got out of control, but I hope you understand it.

TL;DR: Line 3: Use char:GetChildren()['Humanoid'] instead. But also, it’d be better to use something like :FindFirstChild / :WaitForChild, for some cases.

PS.: While I was done writing this, I noticed how this code will only work once. You should listen to when the player’s character is loaded / added, for this will allow the code to work forever. (WRONG!) :

I didn’t fix the issue fully, but it made the sound only play once the player is healing. The gui doesn’t show and the sound only plays on healing not on damage, that is probably something with my wacky script. Thank you tho.

I’m pretty new to coding, I don’t really understand much, but I’ll try to use your feedback on the code, I just don’t really understand how I can add those features to my code. Thank you for the help!

Based on your error message you provided, I see your local script is inside the character. Workspace is not a friendly place for local-scripts, I’d recommend you move it to StarterPlayerScripts.

And as @PhoenixRessusection mentioned, your code will run once. you should use the CharacterAdded event, I’ll start you off:

local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(character) -- Character added!
	local hum = character:WaitForChild("Humanoid") -- Wait for humanoid

	-- Now that we have the humanoid, we can listed to the HealthChanged event
	hum.HealthChanged:Connect(function(health)
		-- your code
	end)

end)

*Wrote this on DevForum, did not test.

It was in StarterCharacterScripts, I mentioned it in the description for the topic, but yeah I’ll try your code, thank you for the help!

1 Like

It didn’t work sadly. The old code with wait for child not get children, made the gui Visible property true but not appear, now the new code doesn’t make it true at all. The sounds are gone. Here is the new code:

local plr = game.Players.LocalPlayer
local char = plr.Character
local hmoid = char:GetChildren("Humanoid")
local onScreen = false

local previousHealth = hmoid.Health

plr.CharacterAdded:Connect(function(character) -- Character added!
	local hum = character:WaitForChild("Humanoid") -- Wait for humanoid

	-- Now that we have the humanoid, we can listed to the HealthChanged event
	hum.HealthChanged:Connect(function(health)
		if health < previousHealth then
			onScreen = true
			script.HitSound:Play()
			game.StarterGui.BloodFlashGui.BloodFlashImage.Visible = true
			wait(1)
			game.StarterGui.BloodFlashGui.BloodFlashImage.Visible = false
			onScreen = false
		end
	end)

end)

EDIT: it’s getting kinda late so I might go to bed before I can test any new code.

local plr = game.Players.LocalPlayer
local char = plr.Character
local hmoid = char:WaitForChild("Humanoid")
local bloodFlash = plr.PlayerGui.BloodFlashGui.BloodFlashImage

local onScreen = false
local previousHealth = hmoid.Health

hmoid.HealthChanged:Connect(function(health)
	if health < previousHealth then
		onScreen = true
		script.HitSound:Play()
		bloodFlash.Visible = true
		wait(1)
		bloodFlash.Visible = false
		onScreen = false
	end
	previousHealth = health
end)

I found two problems. you should do health < previousHealth, because you expect something to happen when the health decreases. Also you should use PlayerGui and not StarterGui because the Gui that the player sees is in PlayerGui. It is also recommended to use WaitForChild to get the Humanoid.

The code will run correctly all the time because it is in StarterCharacterScripts.

4 Likes

Works like a charm! Thank you so much! I’ll keep in mind what you showed me!