Adding Killblocks into an array

Is there any way of making a global kill script file using an Array, housing all the blocks kill scripts rather than having to put the script behind each block? Just thinking it would help with memory.

Thanks

1 Like

You could use a for loop and put the script in any parts that have a certain attribute.

for i, v in pairs(game:GetDescendants()) do
	if v:GetAttribute("killbrick") then
		local killbrickscript = script.Killbrick
		killbrickscript.Parent = v
		killbrickscript.Enabled = true
	end
end
2 Likes

I’ve seen people use this for i,v in pairs, they added all the parts into a folder and renamed them with numbers. Would that be an easier way?

1 Like

Both work, the one i gave would do it to every part with the killbrick attribute.
The kind you mentioned assumes you parent every killbrick in a folder which might not be the best choice but will still work.

2 Likes

It depends how many times the Touched event fires in each kill brick. (this is only my opinion, I may be completely wrong!)

2 scenarios:
A single player game with a whole bunch of killbricks and only the player as an unanchored object.

  • The Touched function would only fire on 1 Part (possibly 2 if they are close enough) which wouldn’t affect lag at all since there only a few Touched events.

A game that has lots of killbricks and many players, with huge amounts of Unanchored Parts that could contact each killbrick.

  • The Touched function would still fire every time a killbrick was touched by anything Unanchored. The script or scripts would still need to check if there was an Attribute or a Humanoid involved and do the damage. A single global script handling the hundreds of inputs from all the killbricks might bog down. You’d probably have better performance with an individual script in each killbrick, and you could insert them as @Overseerzed said.
2 Likes

So you think it would be easier to put it into each block I wanted as a killblock?

1 Like

I don’t advise manually putting the same script in every killbrick incase you need to change something.
Use the for loop and see if that works for you.

2 Likes

You could also use CollectionService for that

just in case if you want to learn more about it

1 Like

I would use CollectionService for this

Open Tag Editor
1
Create a new tag
2
Put it into the parts

Run this script:

local CollectionService = game:GetService("CollectionService") --use the collection service

local killBricks = CollectionService:GetTagged("KillBrick") --get all instances with killbrick tag

for _, killBrick: BasePart in killBricks do --loop over the instances
    killBrick.Touched:Connect(function(otherPart) --connect them to the touched event
        local character = otherPart.Parent --get character

        local humanoid = character:FindFirstChildOfClass("Humanoid") --find humanoid
        if not humanoid then --if humanoid isn't found stop the script from killing
            return
        end

        humanoid.Health = 0 --kill the character that stepped on the brick
    end)
end
4 Likes

What a legend, thank you kindly sir. I’ve learnt something new today. I did always wonder what tags did but never played with them. :slight_smile:

1 Like

I found this way of doing it, a little less code too.

  1. Name all desired blocks in the workspace to “KillBrick”
  2. In ReplicatedStorage - Made a basic on touch kill script.
  3. In ServerScriptService - I set up a For i,v and programmed it to clone the KillScript across all KillBricks on runtime.
1 Like

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