Getting one part from a folder of parts with the same name

Hello, let’s get down to business.

So I essentially want to get all children from a folder and choose one child but all the children have the same name

Here’s what I used:

event.OnServerEvent:Connect(function()
	local parts = partsFolder:GetChildren()
	local randompart = parts[math.random(1, #parts)]
	randompart:Destroy()
end)

The code works however, it selects all the parts instead of just a singular one. If anyone can point me in the right direction that would be great.

Thanks!

that’s picking a singular part tho, maybe its spamming the remote thus it’s basically removing everything?, you can try adding a print to check.

I think that is what is happening actually, but the event only fires once.

image

Maybe I can add a debounce value or something.

other scripts can be firing that remotevent, since u never specified any parameter it’ll fire that remotevent if you have something like this Remote:FireServer(“Parameter1”, “Parameter2”) etc

Well I’ve used the find all panel and only one script fires the event:
image

And only one line of code receives the event:
image

I’ve got nothing in the parameters.

The random part script seems to repeat itself depending on how many parts are in the folder.

show me the script firing the event

Add a debounce in your remote event to prevent double firings.

local event = game.ReplicatedStorage.remoteEvents.RemoteEvent
local clicked = 0
local debounce = 0

game.Players.LocalPlayer.leaderstats.Strength.Changed:Connect(function()
	print("CHANGED")
	event:FireServer()
	print("FIRED")
end)

does that print only once?

Yeah, the print statement in the fireserver script only print one, when fired.

It has to be an issue in the onserverevent script because those print statements are printed for how many parts there are in the folder. But I’m not sure what.

the problem could be the server script, the connection being created multiple times.

Yeah it’s got to be that, do you know what is causing that?

Show the script so we can check, also where is it parented? because if we use debounce in this issue it could cause some performance issue(it might not be noticeable now but who knows it might keep adding up in the future). The problem could also be the parent of the script being cloned thus also cloning the script inside it making it have multiple connections.

event.OnServerEvent:Connect(function()
	local parts = partsFolder:GetChildren()
	local randompart = parts[math.random(1, #parts)]
	randompart:Destroy()
end)

That’s the script.

that’s not inside something like

loop pairs?

Ex:

for i,v in pairs(Workspace:GetChildren()) do
Event.OnServerEvent:Connect(function()

end)
end --- this will create multiple Connection depending on how many parts is in workspace.

You might want to put the whole script, usually multiple connections doesn’t really happen unless it’s caused by some flaw in the other parts of the code

Goddamn life saver. That’s exactly what I did.