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

How do I make my kill part kill only the player who touches it?

local debounce = false

local part = script.Parent
local function onTouched(part)
	if debounce == false then
		
		debounce = true
	
		for i, player in pairs(game.Players:GetPlayers()) do
		if player.PlayerGui:FindFirstChild("Camera"):FindFirstChild("Exit").Visible ~= false then print("Camera is up") else
		player.Character:BreakJoints()
	end
	debounce = false
	end
	end
	end
part.Touched:Connect(onTouched)

I am making an instant death part that only kills players who don't have a certain gui visible on their screen. However, when I tested this in studio with two players it killed them both when only one player touched the part. Does anyone know how to fix this?

1 comment

0

I have been having the same issue, even although the script I’m using is a LocalScript it still runs on every players client

1 answer

✓ Accepted answer

You can do this script, which will only kill the player that touched it. I've already tested it out, and will only kill the player that touched it.

script.Parent.Touched:Connect(function(Hit)
	if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
		Hit.Parent.Humanoid.Health = 0
	end
end)

Basically, inside this function, we're going to check if it has a humanoid, and if it does have a humanoid on the if statement:

if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then

Then the next code would activate, and basically cause the player to die.

Hit.Parent.Humanoid.Health = 0

We're just referencing the health of the humanoid of the player that hit the object.

Basically the problem is, your referencing ALL of the players using GetPlayers, shown in line 9:

for i, player in pairs(game.Players:GetPlayers()) do

which works like GetChildren, but with a bunch of twists and changes, Which is why it's killing ALL of your players once ONE of them touches it. This script that I've shown you that will not kill ALL players once ONE of them touches it, doesn't reference both of what I said that can kill all players.

I hope I have answered this question right.

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