Help with a custom part interaction system

Thanks in advance for the help!

  1. What do you want to achieve?
    I want to create a script (or scripts for that matter) that facilitate interaction between parts. Essentially, most of my parts (I call them cells) have a value (cellValue). If part a hits part b, and part a’s value is greater, I delete part b and give the owner of part a points. I can handle all of the specifics on my own, but I seek advice on how to do this in the best way.

  2. What solutions have you tried so far?
    Currently, each part has a script:

local cell = script.Parent
local value = cell.cellValue
local Players = game:GetService("Players")
local name = cell.Parent.Name
local player = Players:FindFirstChild(name)
local cells = script.Parent.Parent
cell.Touched:Connect (function(part)
	-- Filter any instances of the cell coming in contact with its players other cells
	if part:IsDescendantOf(cells) then return end
	--do stuff
	if part.cellValue then
		if part.cellValue > value then
			local oPlayer = part.Parent.Parent
			local points = oPlayer.leaderstats.points
			points = points + cell.cellValue
			cell:Destroy()
		end
		if part.cellValue == value then
			local oPlayer = part.Parent.Parent
			local points = oPlayer.leaderstats.points
			local playerpoints = player.leaderstats.points
			points = points + cell.cellValue
			playerpoints = playerpoints + part.cellValue
			cell:Destroy()
			part:Destroy()
		end
		if part. cellValue < value then
			local playerpoints = player.leaderstats.points
			playerpoints = playerpoints + part.cellValue
			part:Destroy()
		end
	end
end)

I look at it this way:
One player can have a lot of cells (I’m setting the max around 40). If there are 20 players, that means 80 interaction scripts floating around.

Questions:
Is this the best way of going about this?
Or is there another way of doing this?

Thanks everyone,
Someperson576

1 Like

Actually, there are better ways, you can use collection service. Feel free to go to the Collection Service on the developer hub

2 Likes

Hello again @VegetationBush, and thank you for the information. Do you know any other links about Collection Service?

1 Like

Not that I know of, to help you with collection service, you get the service by using:

local CS = game:GetService("CollectionService")

Collection service is basically a service where you tag parts and then you make all the tagged parts use the same code from only one script!

So here’s a video about collectionService:

Let me know if you have any more questions

2 Likes