Hi there!
I made a script for my difficulty chart obby. This script kill the player when his character touch a part named “KillBrick”. Is it a good idea to put only one script in a character instead put 200+ scripts in KillBrick?
local character = script.Parent
for _, part in ipairs(character:GetChildren()) do
if part:IsA("BasePart") then
part.Touched:Connect(function(hit)
if hit.Name == "KillBrick" then
character.Humanoid.Health = 0
end
end)
end
end
3 Likes
You can also write a KillBrickManager that finds all of your KillBricks and connect each of them to a Touched
event. This script should be server sided for security reasons. Otherwise, people might find ways to hack themselves through your KillBricks (client-side script vulnerability).
The easiest way to find your KillBricks is probably by using the GetDescendants
method.
local obby = workspace:WaitForChild('Obby')
local killBricks = {}
for _, descendant in pairs(obby:GetDescendants()) do
if descendant.Name == 'KillBrick' then
table.insert(killBricks, descendant)
end
end
The fanciest solution is probably to use the CollectionService. See the code example on the bottom of the referenced page to see more information.
3 Likes
I don’t know whether it’s a good idea or not, but in 99% of obbies, it’s always the killbrick’s touched event that kills the player.
I have a question about your script: How many instances your script can check per seconds?
As many as you want. It is a simplified and lazy (which is usually good in scripting) version of
An advantage of this approach is that if you want to make a change to your KillPart, you can just edit the script and every part will still behave the same way. Otherwise, you would have to manually replace those 100+ scripts, which is definitely not desirable.