i am making a obby game so there is a lots of brick thats need to script .if you just put the script in all the brick that will be lag. so i want to make a handler for it . for the example like kill brick i want to put all the kill brick a folder , and make a handler for all the kill brick . so how do i script the handler
put all kill bricks in a folder and do this:
for i, v in pairs(KillbrickFolder:GetChildren()) do
v.Touched:Connect(function(hit)
if game.Players:FindFirstChild(hit.Parent.Name) then
hit.Parent.Humanoid.Health = 0
end
end)
end
This iterates (goes through) all of the kill bricks and checks if they have been touched. If they have, the player is killed.
(the method to kill the player (humanoid.health = 0) is probably depreciated and there’s most likely a function that allows you to kill the player.)
a module script isn’t required in this case as the function isn’t required to be called by any other script.
So long as you have a list of all your kill bricks, you should be able to make it work with 1 script. You can also accomplish using a module script, but that would require a script inside each individual kill brick. Another option is using CollectionService Tags, but this is the easiest way.
local folder = workspace.KillBricks
for _,v in pairs(folder:GetChildren()) do
if v:IsA('BasePart') then
v.Touched:Connect(function(touched)
local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
if possibleHumanoid then
possibleHumanoid.Health = 0
end
end)
end
end
You could also use Collection Service!
As shown in AlvinBox’s Video,
CollectionService is Useful to Get any Amount of Parts under One Tag, to do certain things. (Without Placing Scripts in Each Part, which would cause lag).
Sorry for Replying to you by Accident, Meant the reply for @XxAwesomeDabxX
On top of what these guys are saying, don’t forget to add a Debounce for your touch connection.
where should i put this script?
so the code u give me is just put all the kill brick in a folder and make a script?
if it is then
where should i put the script and what kinda script
Put that script in serverscriptservice and make sure you have all the killbricks in a folder.
Closing as it does not follow the guidelines. Leaving visible as solution was provided.