What's the best way to optimize your game with the following problem:

Sometimes, I do this thing with my game where I have clones of a part which have a script within each one. This can cause problems when there is a bug needed to be fixed in the script, I’d have to change every single script in each part.

What’s the best way to combat this problem?

If you were for example making many KillParts, I would recommend looping through the descendants of the workspace, and check if the part’s name is something like “KillPart”, and fiunction code accordingly.

Let me know if you need any more detail!

Thank you for answering! I don’t know why I haven’t thought of this, this is a great solution to combat a problem like this. This post was mainly to see how other developer’s logic with facing errors in roblox.

If I were to be making an obby, I would use a script similar to this for kill parts.

for i,v in pairs(game.Workspace:GetDescendants()) do
	if v.Name == "KillPart" then
		v.Touched:Connect(function(hit)
			--kill code here
		end)
	end
end

It is a lot easier to identify and fix an error within one script rather than multiple.

1 Like

You can use CollectionService and add tags into Parts. For example, If it’s an Obby (Or something that kills the Players) you can create a “Killer” tag. If you want to know how to create tags and manage them, I recommend you to use TagEditor.

And if you ask, Why is it better to use CollectionService?
Well, CollectionService is a great alternative to organize all your Objects in sections and keep stuff organized. So you don’t need to loop through alot of Objects and do alot of checks. (Name, Properties, etc)

The code for this would look like this:

-- Services
local CollectionService = game:GetService("CollectionService")
local KillerPartTag = "Killer"
local KillerParts = CollectionService:GetTagged(KillerPartTag)

-- Main
for KillerIndex, KillerObject in pairs(KillerParts) do
	if KillerIndex and KillerObject then
		-- We have a KillerObject
		-- Now we can do something with it.
		-- Here's how you would do the kill system:

		-- Connections
		local TouchedConnection = nil
		local AncestryConnection = nil

		-- Touched Connection
		-- This is gonna hold the Touched Connection
		-- Whenever something touches the Object.
		TouchedConnection = KillerObject.Touched:Connect(function(Hit)
			if Hit and Hit.Parent then
				-- If something has hit the Object and it has a parent
				-- We're gonna run this
				local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
				if Humanoid and Humanoid.Health > 0 then
					-- We have a Humanoid and it's alive!
					-- Meaning that we can modify it/kill!
					Humanoid.Health = 0
				end
			end
		end)

		-- Ancestry Connection
		-- This is gonna hold the Connection whenever
		-- The Object Ancestrys' (Parent) Changes
		AncestryConnection = KillerObject.AncestryChanged:Connect(function(Child, Parent)
			if not Parent then
				-- The part was destroyed
				-- We have to Clean-up the connections!
				TouchedConnection:Disconnect()
				AncestryConnection:Disconnect()
				TouchedConnection = nil
				AncestryConnection = nil
			end
		end)
	end
end
3 Likes

If you ever have instances which share the same copy of some script then you should attempt to rewrite the script such that it works for all of the instances with only a single copy of the script being required. The first reply to the thread outlined a good example (a single master script controlling all kill parts as opposed to each kill part having its own copy of some script).

1 Like