Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to know if there is 2 of the same part?

I want to know how know if there is 2 of the same part. I am making a little puzzle that the player needs to solve by pushing around the ball. My code:

local toggle = false
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not toggle then
			toggle = true
			script.Parent.BrickColor = BrickColor.new("Really black")
			script.Parent.Material = ("Plastic")
			local ball = Instance.new("Part",game.Workspace)
			Instance.new("Part",game.Workspace)
			ball.Shape = Enum.PartType.Ball
			ball.Position = Vector3.new(-24, 110, 0)
			ball.BrickColor = BrickColor.new("Institutional white")
			ball.Material = ("SmoothPlastic")
			ball.Size = Vector3.new(10, 10, 10)
			ball.Name = ("Ball")
			game.Workspace.Ball:Destroy()
			wait(5)
			script.Parent.BrickColor = BrickColor.new("Lime green")
			script.Parent.Material = ("Neon")
			toggle = false
		end
	end
end)

I want to make it so if there is 2 balls then the script will delete one ball. But I don't know how to count how many balls there is and there is one problem were if there is no balls like if it falls off the map and I respawn it will just delete it self. How do I fix this?

2 answers

Before you spawn the first ball, delete the second one. If these balls are bound to a specific player you may want to make a way to tag the player with it. Perhaps with a custom property in the ball.

2 comments

0

the balls do not go with a specific player i am making a little puzzle that the player needs to solve by pushing around the ball

0

Then just delete the ball before spawning the next one. Search for it in the workspace with the index to variable for loop (EX: 'for i,v in pairs(workspace:GetChildren()')

You can try checking every instance in the workspace, and if it matches the name of the balls, just add it to a table, and do whatever you wanted to!

local children = workspace:GetDescendants()

local balls = {}
local counted = 0

for _,v in pairs(children) do
      if v.Name == "ball name here" then
           counted = counted + 1
table.insert(balls,counted,v)
     end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.