Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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.

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 IceNinja225500 8 — 3y
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()') itchymoonfire 179 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

Answer this question