Part only killing player when walking through it

I’m trying to make a script that will kill the player if the part is touched. The problem is that if the player stands still, the part wont kill them.

Here’s what I mean,
robloxapp-20240227-1749319.wmv (989.8 KB)

Here’s the code,

script.Parent.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if (humanoid ~= nil) and script.Parent.Transparency == 0 then	
		humanoid.Health = 0	
	end
end)

Is there anyway to fix this?

2 Likes

Try doing

If humanoid and humanoid.Health > 0 then

Instead.

1 Like

I think this might be from the character collision type. I think there is a setting in the game settings menu that changes the character collision type.

For example, I know a few games had a problem where people with the levitate animation didn’t take damage because their feet weren’t touch the ground.

There are more hacky ways to fix this, such as adding a custom hitbox to the character or adding an invisible non-collide part with the kill script above the real kill part.

script.Parent.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and script.Parent.Transparency == 0 then	
		humanoid.Health = 0	
	end
end)

A good thing to use here would be GetPartsInParts(). I made a formula for you.

local RunService = game:GetService("RunService") -- Creates the service in case if the game hasn't loaded it yet to prevent errors.

local RBXScriptConnection = nil -- Important later. This variable has been assigned to nil for now.

local Params = OverlapParams.new() -- Important raycasting parameters.
Params.MaxParts = 100 -- How many instances the raycast can detect. This is useful, because too many instances being detected can cause huge lag spikes.
Params.FilterDescendantsInstances = {script.Parent} -- Raycast filter. Basically what instances the raycast should ignore. This too can prevent lag spikes and can speed up this entire process.

RBXScriptConnection = RunService.Heartbeat:Connect(function(deltaTime) -- Assigning our variable from earlier to this script connection in case if RBXScriptConnection:Disconnect() needs to be called.
	local PartsInPart = workspace:GetPartsInPart(script.Parent, Params) -- Casts the ray to detect what parts are in script.Parent's vicinity/area.
	
	for i, v in PartsInPart do -- Goes through all of the parts the raycast returned.
		if v:FindFirstAncestorOfClass("Model") then -- If the part's parent is a model, then...
			local Character = v.Parent
			local Humanoid = Character:FindFirstChildOfClass("Humanoid")
			
			if Humanoid and Humanoid.Health > 0 then
				Humanoid.Health = 0
			end
		else -- If the part's parent isn't a model, then...
			Params:AddToFilter(v) -- Adds the part to the filter from earlier.
		end
	end
end)

-- And there you have it. Hope this helped!

Albeit the script takes more time to program than a single Touched event, but this is necessary for your case.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.