Need help with a Kill Part

Hello developers!

Today when I tried to make a Kill Brick that sets the health of the player who touched it to 0.

But I noticed that when a player keeps jumping the script will not detect the touch event.

Here is an example:

robloxapp-20210221-1001078.wmv (2.4 MB)

I tried making the Kill Part’s CanCollide property to true and it worked, but i wonder if there is a way to fix this issue without doing that.

Here is the code i used:

local killPart = script.Parent -- Kill part position

killPart.Touched:Connect(function(hit)
	local char = hit.Parent -- Character
	local hum = char:FindFirstChild("Humanoid") -- Humanoid
	if hum then -- If humanoid then...
		if hum.Health ~= 0 then -- Makes sure that character is not dead
			hum.Health = 0 -- Kills the character
		end
	end
end)

Thank you for reading and trying to help!

3 Likes

Could You Try to Print Something When the Player Touches the Part ? Just After .Touched:Connect(function(hit)

Once You did that Try To Keep Jumping and go over the Part and Check if it Prints anything or not.
This will make us know that when the player keeps on jumping, Does the Touch event actually fire or not

1 Like

I have a small suggestion that could work if the event actually fires, make it do a loop, so that way if the input stops, the script will still do the function through the loop.

1 Like

Hello @YT_Forceman , I tried updating the code to this:

local killPart = script.Parent -- Kill part position

killPart.Touched:Connect(function(hit)
	print("Part has beed touched")
	local char = hit.Parent -- Character
	local hum = char:FindFirstChild("Humanoid") -- Humanoid
	if hum then -- If humanoid then...
		if hum.Health ~= 0 then -- Makes sure that character is not dead
			hum.Health = 0 -- Kills the character
		end
	end
end)

But when i keep jumping on the part nothing prints on the output

1 Like

Use localscripts instead of server scripts

2 Likes

No, LocalScripts only work on client, server scripts work on server, he cannot use a local script literally let me think about this for a bit, and I will try to come up with an answer

1 Like

I am sorry?

Killparts in every major obby are local,
in fact, they are local as in they used a Touched connection on either the humanoid or a custom hitbox for the player.
After all, servers dont get the real time position as replication works in 20hz.

3 Likes

Hello @Feedekaiser , I tried replacing the Server Script by a Local Script, but actually nothing happens, maybe because the local scripts don’t work in Workspace.

Thanks for replying.

Put them in StarterPlayerScripts and reference part with workspace.Part

4 Likes

@Amine360YT I tried using your script in roblox studio, attached to a part and it worked for me? Might be a problem on your end

1 Like
local killPart = script.Parent;
killPart.Touched:Connect(function(Part)
  if Part.Parent:FindFirstChild("Humanoid") ~= nil then
    Part.Parent.Humanoid.Health = 0;
  end
end)
2 Likes

@Feedekaiser , I tried your solution and it actually worked!

Here is the script inside the StarterPlayerScripts:

local killPart = game.Workspace.KillPart

killPart.Touched:Connect(function(hit)
	print("Part has beed touched")
	local char = hit.Parent -- Character
	local hum = char:FindFirstChild("Humanoid") -- Humanoid
	if hum then -- If humanoid then...
		if hum.Health ~= 0 then -- Makes sure that character is not dead
			hum.Health = 0 -- Kills the character
		end
	end
end)

Thanks for everyone replied to this topic!

1 Like

@Amine360YT

local killPart = script.Parent
local function onPartTouch(hit)
local hum = hit.Parent.Humanoid
if hum then
	hum.Health = 0 
end
print("Part has been touched!")
end

killPart.Touched:Connect(onPartTouch)

I made a slightly simpler version.

1 Like

Ok so

After looking at this I found a good idea of what you could’ve try

Note I didnt make the script

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:BreakJoints()
	end
end)

But you could make it do a loop by doing this

script.Parent.Touched:Connect(function(hit)
       While true do
                Wait()
	        if hit.Parent:FindFirstChild("Humanoid") then
		       hit.Parent:BreakJoints()
	      end
       end
end)

Putting the script for controlling it, in starterplayerscripts has a chance to get exploited

if you index an empty field or nonexistent child it will error, therefore it makes no sense to check if hum exist at all.

@Feedekaiser , I have a question:

Should i copy that script every time I make a new kill part?

Should not be neccessary. Nor should this system be a local script as well. Having this in serverscript service as a server script should be fine. Though server scripts can run in workspace as well so that is not the problem. I would recomend using functions such as break joints to make sure you actually kill the player. If the script is a server script and is located either in server script service or the workspace this script should work fine though

This is a bad idea as it creates a new loop every time the part is hit, not to mention it doesn’t yield at all, which will crash roblox.

Fair enough but yours wont work either, he will have to make a nee script every time a nee one is created, due to the fact that if the parts are the same name roblox will not be able to find the actual one yout referenceing

No
if you have multiple killparts, consider using something like this

game.Players.LocalPlayer.CharacterAdded:Connect(funcition(character)
  character:WaitForChild'Humanoid'.Touched:Connect(function(x)
    if x.Name == 'KillPart' then
     character:BreakJoints()
    end
  end)
end)
1 Like