How to check for multiple parts/models in workspace?

Is it possible to check for multiple parts or models in workspace that have the same name?
In the code below I have my tape placement system I want the player to only place a certain amount. When the player places one its name: PlayerUsernameTape. So how can I test if there are 5 of those in my if statement?

TapeHandler.OnServerEvent:Connect(function(Player, Argument, Start, End)

	if not Argument then return end

	if Argument == "PlaceTape" then

		if workspace:WaitForChild(Player.Name.."Tape", 5) then --if statement

			print("nope")
2 Likes

Where are you parenting the tape object? I suggest parenting them to a folder called PlayerTape.

Using that suggestion, you can easily find the amount of tape with this function:

local function AmountOfTape(Player)
	local Amount = 0
	
	for i,v in workspace.PlayerTapeFolder do
		if v.Name == Player.Name then
			Amount += 1
		end
	end
	
	return Amount
end

if AmountOfTape(Player) < 5 then
	--do code that allows them to place more tape

    Tape.Parent = workspace.PlayerTape --> this way you can keep track of the player tape.
end
1 Like

I would recommend storing the tape in an array, that way you can both count and reference them directly. However, to answer the other part of your question, you can iterate through the workspace with a for loop and either workspace:GetChildren() or workspace:GetDescendants().

1 Like

I did the following but now the tape doesn’t place any more and there are no errors

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TapeHandler = ReplicatedStorage:WaitForChild("TapeHandler")



TapeHandler.OnServerEvent:Connect(function(Player, Argument, Start, End)

	if not Argument then return end

	local function AmountOfTape(Player)

	local Amount = 0



	for i,v in workspace.PlayerTapeFolder do

		if v.Name == Player.Name then

			Amount += 1

		end

	end



	return Amount

end



if AmountOfTape(Player) < 5 then

	--do code that allows them to place more tape

		if Argument == "PlaceTape" then

			local Part = script.TapePart:Clone()

			Part.CanCollide = false

			Part.Anchored = true

			Part.Color = Color3.fromRGB(255, 255, 0)

			Part.Material = Enum.Material.Plastic

			Part.Name = Player.Name.."Tape"

			Part.CFrame = CFrame.new(Start, End)

			Part.Position = (Start + End) / 2

			Part.Size = Vector3.new(0.1,1, math.abs((Start - End).Magnitude))

			Part.Parent = workspace

			TapeHandler:FireClient(Player, "Enable", Part.ProximityPrompt)

			Part.ProximityPrompt.Triggered:Connect(function(proxPlayer)

				if Part.Name == proxPlayer.Name.."Tape" then

					Part:Destroy()

					local Tape = game.Workspace.PlayerTapeFolder

					Tape.Parent = workspace.PlayerTape --> this way you can keep track of the player tape.

				end

			end)

		end

	end

end)


Sorry for large spacing formatting went weird

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TapeHandler = ReplicatedStorage:WaitForChild("TapeHandler")

TapeHandler.OnServerEvent:Connect(function(Player, Argument, Start, End)
	if not Argument then return end

	local function AmountOfTape(Player)
		local Amount = 0

		for i,v in workspace.PlayerTapeFolder do
			if v.Name == Player.Name then
				Amount += 1
			end
		end

		return Amount
	end

	if AmountOfTape(Player) < 5 then
		print("CanPlaceTape")
		
		if Argument == "PlaceTape" then
			local Part = script.TapePart:Clone()
			Part.CanCollide = false
			Part.Anchored = true
			Part.Color = Color3.fromRGB(255, 255, 0)
			Part.Material = Enum.Material.Plastic
			Part.Name = Player.Name.."Tape"
			Part.CFrame = CFrame.new(Start, End)
			Part.Position = (Start + End) / 2
			Part.Size = Vector3.new(0.1,1, math.abs((Start - End).Magnitude))
			Part.Parent = workspace

			TapeHandler:FireClient(Player, "Enable", Part.ProximityPrompt)

			Part.ProximityPrompt.Triggered:Connect(function(proxPlayer)
				if Part.Name == proxPlayer.Name.."Tape" then
					Part:Destroy()

					local Tape = game.Workspace.PlayerTapeFolder
					Tape.Parent = workspace.PlayerTape --> this way you can keep track of the player tape.
				end
			end)
		end
	else
		warn("CantPlaceTace")
	end
end)

Try this code and tell me what appears in your output.

1 Like

I keep getting this error in the output
ServerScriptService.Server:12: attempt to iterate over a Instance value

Try changing for i,v in workspace.PlayerTapeFolder do to for i,v in ipairs(workspace.PlayerTapeFolder:GetChildren()) do

1 Like

It kinda works that fix. No errors it will let me place the tape, but the limited of 5 won’t work. It lets me place as much as i want instead of 5 max.

Maybe the amount value isn’t updating. Maybe try changing if v.Name == Player.Name then (the line after the for loop line) to if v.Name == Player.Name..”Tape” then
Sorry for the late reply

that’s alright. um I tried doing so and it doesn’t work at all now.
Just making sure I did correctly here is where I put it.

local function AmountOfTape(Player)

		local Amount = 0



		for i,v in ipairs(workspace.PlaceTapeFolder:GetChildren()) do

			if v.Name == Player.Name then

				if v.Name == Player.Name.."Tape" then

				Amount += 1

Sorry for big spacing my formatting goes weird

English isn’t my first language so my fix might have been unclear. What my fix should look like:


for i,v in ipairs(workspace.PlaceTapeFolder:GetChildren()) do

			if v.Name == Player.Name..”Tape” then
				Amount += 1
			end
		end

Full function:


local function AmountOfTape(Player)
		local Amount = 0

		for i,v in ipairs(workspace.PlaceTapeFolder:GetChildren()) do

			if v.Name == Player.Name..”Tape” then
				Amount += 1
			end
		end

		return Amount
	end

2 Likes

It fixed it thank you so much for all your help!!

2 Likes

Thank you for all your help too!!

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