Okay, here's a script that will kill a player when their character touches the Part this script is inside.
How to use it:
- Create a Part: In Roblox Studio, insert a new Part into your Workspace (or wherever you want the "kill brick" to be). You can customize its appearance, size, and CanCollide property.
- Insert a Script: Right-click on the Part you just created in the Explorer window, and select
Insert Object > Script.
- Paste the Code: Open the new Script and paste the code below into it.
-- Get the Part this script is inside of
local killPart = script.Parent
-- A table to keep track of Humanoids recently touched to prevent rapid multi-kills from one touch
local debounces = {}
local DEBOUNCE_TIME = 0.5 -- Half a second debounce per player
-- Function to handle what happens when something touches the part
local function onTouch(otherPart)
-- Check if the thing that touched is part of a character model
local character = otherPart.Parent
if not character then
return -- Not part of a character (e.g., a loose part)
end
-- Try to find a Humanoid in that character model
local humanoid = character:FindFirstChildOfClass("Humanoid")
-- If a Humanoid exists and it's alive
if humanoid and humanoid.Health > 0 then
-- Check if this humanoid is currently debounced
if debounces[humanoid] then
return -- Already processed this humanoid recently
end
-- Add to debounce table
debounces[humanoid] = true
-- Kill the Humanoid
print(character.Name .. " touched the kill brick!") -- Optional: for debugging
humanoid.Health = 0
-- Remove from debounce table after a short delay
task.wait(DEBOUNCE_TIME)
debounces[humanoid] = nil
end
end
-- Connect the 'onTouch' function to the 'Touched' event of the killPart
killPart.Touched:Connect(onTouch)
print("Kill brick script loaded for: " .. killPart.Name)
Explanation:
local killPart = script.Parent: This line gets a reference to the Part that the script is a child of.
local debounces = {}: This creates an empty table. We'll use this to store Humanoids that have recently been "killed" by this part. This prevents the script from trying to kill the same player multiple times in rapid succession if multiple body parts touch the kill brick almost simultaneously.
local DEBOUNCE_TIME = 0.5: Sets how long (in seconds) a player is "immune" to being re-killed by this specific brick after an initial touch.
local function onTouch(otherPart): This defines a function that will be called whenever something touches killPart. The otherPart parameter is the specific BasePart (like an Arm, Leg, or Torso) that made contact.
local character = otherPart.Parent: We assume the otherPart (e.g., "RightFoot") is a child of the character's Model.
local humanoid = character:FindFirstChildOfClass("Humanoid"): We then look inside that character Model for an object of the class "Humanoid". All player characters (and NPCs with health) have a Humanoid.
if humanoid and humanoid.Health > 0 then: This checks two things:
humanoid: Does a Humanoid actually exist in the model? (This prevents errors if, say, a loose brick touches the killPart).
humanoid.Health > 0: Is the Humanoid currently alive? No point in "killing" something that's already dead.
if debounces[humanoid] then return end: If this specific humanoid is already in our debounces table (meaning it was just processed), we stop the function early.
debounces[humanoid] = true: If not debounced, we add this humanoid to the debounces table to mark it as processed.
humanoid.Health = 0: This is the line that actually kills the character by setting its health to zero.
task.wait(DEBOUNCE_TIME): Pauses execution for this specific thread for DEBOUNCE_TIME seconds.
debounces[humanoid] = nil: After the debounce time, we remove the humanoid from the table, allowing them to be killed by this brick again if they touch it.
killPart.Touched:Connect(onTouch): This is the crucial line that connects our onTouch function to the Touched event of the killPart. Whenever killPart is touched, the onTouch function will be executed.
Now, when any part of a player's character touches the Part containing this script, their health will be set to 0, and they will die and respawn (if your game has default respawning enabled).