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

How to make the lava only kill the player?

I am trying to make a simple zombie round based survival game, I need to make the lava only kill the player and not any other humanoid. Any suggestions?

function onTouch (part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if (humanoid ~= nil) then
	    humanoid.Health = 0 
	    local ms = Instance.new
	    ms.Text = "Do not step outide the base!"
	    ms.Parent = workspace
	    wait(5)
	    ms:remove()
	end
end

	script.Parent.Touched:connect(onTouch)

The message plays even though a zombie touches the brick. The goal of the game is to stay in your bunker while shooting zombies all the time.

4 comments

0

What you could do is make a "RemoteEvent" inside the part, and fire it when a player touches it.

0

no, that's a bit too complicated

0

Get the player’s character through part.Parent and use :GetPlayerOnCharacter(char) on the Players service to check if the character is a player.

0

there is a much easier way, I'll explain

1 answer

✓ Accepted answer

Like Denny said, you can tell if the thing that touched the part was a player using GetPlayerFromCharacter():

script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
	    local player = game.Players:GetPlayerFromCharacter(part.Parent)
	    local hum = part.Parent:FindFirstChild("Humanoid")
		if player then
			hum.Health = 0
		end
	end
end

The method in this case is literally referencing the player's Character.

3 comments

0

Thank you so much

1

why not declare the humanoid variable at line 2, instead of repeating yourself on line 5 :smirk:

0

oof good point

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