Need help with functions and events

Hey, I’m trying to make a lava brick that kills you when you touch it, I’ve followed the tutorials on the Roblox DevHub, and yet this doesn’t seem to be working…


local function killPlayer(touchedPart)
	print ("Function 'killPlayer' ran.")
	local partParent = touchedPart.Parent
	local humanoid = partParent:FindFirstChild("Humanoid")
	if humanoid == true then
		humanoid.Health = 0
	end
end

brick.Touched:Connect(killPlayer)

I suppose that this goes under the “extremely basic” part of the scripting support category… oh well. Any help would be appreciated, thank you!

you must make sure it is not a local script but a script first then try simplify it to understand it more like:

local function killPlayer(hit)
	print ("Function 'killPlayer' ran.")
	local Char = hit.Parent
   if Char:FindFirstChild("Humanoid") then
	local humanoid = Char:FindFirstChild("Humanoid")
	
		humanoid.Health = 0
	end
end

brick.Touched:Connect(killPlayer)
1 Like

Try this?

local function killPlayer(touchedPart)
	print ("Function 'killPlayer' ran.")
	local partParent = touchedPart.Parent
	local humanoid = partParent:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	humanoid.Health = 0
end

brick.Touched:Connect(killPlayer)
1 Like

alright, ill try that, thank you

Make sure that ‘brick’ variable exists. If this is a normal script inside the part, do this:

local brick = script.Parent

2 Likes

it does, sorry, i forgot to include it when i pasted the script

So that would mean a script that looks like this(full script):

local brick = script.Parent

local function killPlayer(hit)
	print ("Function 'killPlayer' ran.")
	local Char = hit.Parent
	if Char:FindFirstChild("Humanoid") then
		local humanoid = Char:FindFirstChild("Humanoid")

		humanoid.Health = 0
	end
end

brick.Touched:Connect(killPlayer)
1 Like

yes, can you explain to me why there is a space between line 7 and 9??

It must be a bug, but spaces in the lines don’t matter.

1 Like

Wouldn’t it be simpler to do this?

local brick = script.Parent

local function killPlayer(hit)
	print ("Function 'killPlayer' ran.")
	local Char = hit.Parent
	local humanoid = Char:FindFirstChild("Humanoid")
	if not humanoid then return end
	humanoid.Health = 0
end

brick.Touched:Connect(killPlayer)

Edit: replied t othe wrong person, meant to reply to @AC_Starmarine

I tested the code in studio, your code works just fine @Precisero

huh? didnt work for me… i will look again

Are you sure that the script that this code is in is a normal script? not a local script. Also that the script’s parent is in the brick you want to touch to kill you?

Did you put a regular script with that code inside of the brick?

Yes. It’s a server script. The server script is parented to the brick.

Just like this:
image

I tested my code again, and it isn’t working

Are you receiving any errors when testing? Did you copy the code correctly?

Is it like the image? and are you sure there is nothing else in the script or any errors?

No errors. I copied the code perfectly too!