Hello,
I need some help to learn how to make a kill brick. I have tried this so far:
Script
local damageBrick = script.Parent
function onTouched(hit)
local character = hit.Parent
local humanoid = character:FindFirstChildOfClass(“Humanoid”)
if humanoid then
humanoid.Health = humanoid.Health - (humanoid.MaxHealth * 0.25) – Reduce health by a quarter
end
end
damageBrick.Touched:Connect(onTouched)
It just doesn’t work how I thought it would.
I have used different bricks too but it didn’t work. Also for context, my friend helped me with it.
What were you hoping for it to do v.s. what it currently does? Is your goal to kill them instantly, or are you hoping to do damage every time a player touches it?
local debounce = false
script.Parent.Touched:Connect(function(hit)
if debounce then return end
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
debounce = true
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
humanoid.Health = humanoid.Health - (humanoid.MaxHealth * 0.25)
task.wait(1)
debounce = false
end
end)
Pretty much spoonfed, but let me explain since you basically have the correct idea.
The way you’re doing it would most likely kill instantly, judging by your code you want it to just take part health away and not kill instantly.
You should actually check if it’s fully a player which is this line here;
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
then do the if player then --code-- end
I pretty much just re-got the player’s humanoid, and set it’s health just how you did, but instead of using the FindFirstChildOfClass, I just made it find the first thing called Humanoid (because there should only ever be 1 Humanoid inside)
I added the debounce because it would kill instantly, as the Touch event would fire like every milisecond if you stand on it.
Here’s an example I’ve put together with comments for you. Please do read the comments & check out the links I added in there though, I think they would help you a lot.
To use the following example:
Insert a Part
Insert a Script inside that Part
Copy and paste the code below inside the script
You’re ready to go
Example code
-- use the parent of this script as the `Damage Brick`
-- you can learn more about the datamodel here: https://create.roblox.com/docs/projects/data-model
local damageBrick = script.Parent
-- i.e. wait at least 1 second before damaging people who
-- have touched us - FEEL FREE TO CHANGE THIS
local damageDebounceTime = 1
-- i.e. the amount of damage we want to take every time
-- a player hits the damageBrick - FEEL FREE TO CHANGE THIS
local damageAmount = 10
-- debounce table
--
-- We use this to make sure we're only taking damage
-- every few seconds
--
-- i.e. this tracks all the `Humanoids` that have
-- had damage applied to them recently and
-- records the last time at which we damaged them
--
-- you can learn more about tables here: https://create.roblox.com/docs/luau/tables
--
local debounce = { }
-- note: the setmetatable({ }, { __mode = 'k' })
-- stuff isn't all that necessary
-- but it lets the humanoids get garbage collected
-- once the player dies.
--
-- you can learn more about that here: https://www.lua.org/pil/17.html
debounce = setmetatable({ }, { __mode = 'k' })
-- a function to handle the incoming `Touched` events for the `Damage brick`
-- where `hit` is the instance that touched us
--
-- learn more about this here: https://create.roblox.com/docs/reference/engine/classes/BasePart#Touched
--
local function onTouched(hit)
-- do nothing if our `hit` part no longer exists
-- or if it doesn't have a parent
if not hit or not hit.Parent then
return
end
-- find a `Humanoid` instance within the parent of the `hit` part
-- you can find out more about querying the world / instances here:
-- Link: https://create.roblox.com/docs/reference/engine/classes/Instance#FindFirstChildOfClass
--
local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
if humanoid then
-- check to see if we damaged them recently
-- if we have then we will check to see if
-- at least n seconds have passed since the last
-- damage (as defined by damageDebounceTime)
local timeNow = os.clock()
local lastDamageTime = debounce[humanoid]
if lastDamageTime and timeNow - lastDamageTime < damageDebounceTime then
return
end
-- update the debounce time for the next check
debounce[humanoid] = timeNow
-- now apply the damage
-- learn more about the humanoid here:
-- - https://create.roblox.com/docs/reference/engine/classes/Humanoid
humanoid:TakeDamage(damageAmount)
end
end
-- listens to the `Touched` event and calls the `onTouched`
-- method when a signal is received
--
-- learn more about this here:
-- - Events link: https://create.roblox.com/docs/scripting/events/built-in
-- - TouchedEvent link: https://create.roblox.com/docs/reference/engine/classes/BasePart#Touched
--
damageBrick.Touched:Connect(onTouched)
P.S. Roblox has their own tutorials including a Kill Brick tutorial. If you’re just starting out I would recommend you check them out here.