How to create a Kill script part

I am a beginner at scripting, so please don’t judge me. I need help creating a kill part script for an obby that I am making. I am creating this obby to gain a better understanding of scripting, and I plan to take on more complex projects once I have a better understanding of the basics of scripting. I would appreciate it if someone could explain what each part of the script does, rather than just giving me the script itself.

1 Like

assuming the script is under a part

-- (script) is the Script itself and (script.Parent) is its parent (which would be the part)
-- Here, we connect to a touch event, every time something touches the parent of the script the event gets fired. 
script.Parent.Touched:Connect(function(Toucher)
-- Toucher is the variable for the thing that touched the part
-- Now since anything can touch the part, we put an if statement to see if the parent of the thing that touched the part has a humanoid (all player character has this)
    if Toucher.Parent:FindFirstChild("Humanoid") then
-- If it does, it will damage the thing (player character) that touched it.
       Toucher.Parent.Humanoid:TakeDamage(100)
    end
end)
-- We used (Toucher.Parent) instead of just (Toucher) because player characters are models, and only the parts under that model can fire the event / be detected by the touch signal
-- Get a reference to the Brick object where this script resides
local Brick = script.Parent

-- Define a function named PlayerTouched that takes an argument 'hit', which represents the object that touched the Brick
local function PlayerTouched(hit: BasePart)
	-- Check if the object that touched the Brick has a parent that is a Model (assuming it's a character)
	local char = hit.Parent:: Model
	
	-- Attempt to find the child 'Humanoid' object within the character
	local humanoid = char:FindFirstChild("Humanoid"):: Humanoid
	
	-- If 'Humanoid' object is not found, exit the function
	if not humanoid then
		return	
	end
	
	-- Set the health of the humanoid to 0, effectively "killing" the player
	humanoid.Health = 0
end

-- Connect the PlayerTouched function to the 'Touched' event of the Brick object
Brick.Touched:Connect(PlayerTouched)

1 Like

An even better way to manage multiple killparts is with one script!

Put all your killparts under a folder in the workspace, and call it “KillParts”.

Then, put a script in ServerScriptService:

local players = game:GetService("Players")
local killParts = workspace.KillParts

for _, part in ipairs(killParts:GetChildren()) do
    part.Touched:Connect(function(hit)
        local player = players:GetPlayerFromCharacter(hit.Parent)
        if player then
            player.Character.Humanoid.Health = 0
        end
    end)
end
3 Likes

A basic way of doing a kill part is when part is touched, if its a player, it kills it.

script.Parent.Touched:Connect(function(hitPart)
	if game:GetService("Players"):GetPlayerFromCharacter(hitPart.Parent) then
		hitPart.Parent:FindFirstChildOfClass("Humanoid").Health = 0
	end
end)
2 Likes

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