Hello! I’m messing around the kill brick script that i made but somehow the output has no sign of error and the script isn’t working is there any solution, also please kindly explain to me the issue I am facing so I can get a better understanding of it and learn from my mistake.
script.Parent.Touched:Connect(function(OtherPart)
local player = game.Players:GetPlayerFromCharacter(OtherPart)
if player then
player.Character.Humaniod.Health = 0
end
end)
script.Parent.Touched:Connect(function(OtherPart)
local player = game.Players:GetPlayerFromCharacter(OtherPart)
if player then
player.Character.Humaniod.Health = 0
end
end)
script.Parent.Touched:Connect(function(OtherPart)
local Humanoid = OtherPart.Parent:FindFirstChild("Humanoid")
if Humanoid then
Humanoid.Health = 0
end
end)
:GetPlayerFromCharacter() never really works for .Touched events
To reduce the amount of method calls, you should probably check for Humanoid and then check if it’s Player instance.
script.Parent.Touched:Connect(function(OtherPart)
local Humanoid = OtherPart.Parent:FindFirstChild("Humanoid")
if Humanoid then
local Player = game:GetService("Players"):GetPlayerFromCharacter(OtherPart.Parent)
if Player then -- This will only kill Players. Not NPCs.
Humanoid.Health = 0
end
end
end)
local function onTouched(hit)
local model = hit:FindFirstAncestoryWhichIsA("Model")
local humanoid = model:FindFirstChildWhichIsA("Humanoid")
if humanoid then
model:BreakJoints()
end
end
the simplest way I know to make a killbrick script is this:
script.Parent.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
Humanoid.Health = 0
end
end)
--note that if you had an NPC in your game, this would kill it
--as well if touched, so if you have a setup like that, you should
--add a check for players in between.
What I would recommend for kill bricks, is using Collection Service and Tagging all the Kill bricks as something like Kill Bricks and then in A Server Script, you loop through all the objects tagged as Kill Bricks and basically use something like this script
for _, killBrick in pairs(CollectionService:GetTagged("Kill Bricks")) do
killBrick.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Basically check if its a player or not.
if player then
player.Character.Humanoid.Health = 0
end
end)
end
I use Collection Service for two main reasons:
Its easier, because you don’t need to keep pasting scripts for new kill bricks, just tag it and done.
If you have any bugs, in the code you would have to edit each and every script, but if you use Collection Service, you just need to change that one script.
You tagged them with Collection editor? If not then can you tell me how are you tagging them? Also I just gave that as an example, didn’t mean to spoon feed you.
Just a suggestion anyways if you find a script in someone’s solution try to debug it if something isn’t working, because it seems to be working for me.
Well in your part create a script and enter that:
script.Parent.Touched:Connect(function(hited)
if hited and hited.Parent and hited.Parent:FindFirstChild(“Humanoid”) then
hited.Parent.Humanoid.Health = 0
end
end)