Using collection service with text buttons

Hey, im trying to make a single script that works for multiple buttons in this case I wanted to check or it worked for only the click sound only it I believe it is because the script isnt inside of a button but I still dont totally get it here is my script

local CS = game:GetService("CollectionService")

local function MouseClick(button)
	
	button.MouseButton1Up:Connect(function()
		local ClickSound = button.Parent.ClickSound
		ClickSound:Play()
	end)
	

end

for _, buttons in pairs(CS:GetTagged("MenuButton")) do
	MouseClick(buttons)
end

tbh I never really worked with this server before and I hope someone could help me out

1 Like

Hey @Allard2019! Have you tried reading this article on the Roblox documentation on the collection service?

Could you explain what isn’t working here?

well as I know it kinda works like a table only idk how to choose the labeled option and make that the button in this function since the script is in the server script service

local function MouseClick(button)
	print("yoyo")
	button.MouseButton1Up:Connect(function()
		print("hi")
		local ClickSound = button.Parent.ClickSound
		ClickSound:Play()
		print("lol")
	end)

if I would let the in pairs loop run this it would only print yoyo this is because I dont know how to choose the textbutton which is labeled so it would execute the mousebutton1up that action from the player is now linked to a idk prob a nil I dont get any errors so thats why im asking it here

sorry let me be more clear my problem it it loops right through the buttons so it prints in this case 5x yoyo bc there are 5 tagged also sicne i do not click it while it loops through the buttons it doesnt print lol or hi is there a way I can fix this so the function only runs when I click on 1 of the button thats tagged or is this impossible?

Are you sure it doesn’t work when you click one of the buttons? it should work just fine. it prints yoyo 5 times because there are 5 buttons, and it connects a mouseclick “listener” for each one of them individually, meaning it listens to when a button gets clicked, and the function runs.

Does it not do anything when you click one of the buttons?

Sorry for any late responses, I have to go soon

try this.

for i,v in pairs(game:GetDescendants()) do
    if v:IsDescendantOf(game.Players.LocalPlayer.PlayerGui.Menu.Buttons) then
    button.MouseButton1Up:Connect(function()
    button.Parent.ClickSound:Play()
    end
end

he’s trying to use collectionservice, not for loops… the former is better

yeah well I actually am skilled with the in pairs loop only ive never used it to run when a textbutton thats inside of the table is clicked and yes I know the error is in the mousebutton1up I think the mousebutton 1Up isnt linked right

local CS = game:GetService("CollectionService")
local function MouseClick(button)
	button.MouseButton1Up:Connect(function()
		local ClickSound = button.Parent.ClickSound
		ClickSound:Play()
	end)
end

for _, buttons in pairs(CS:GetTagged("MenuButton")) do
	MouseClick(buttons)
end

if you take a look at that script it says Mouseclick(button) which makes a variabel so when I recall the function I use MouseClick(buttons) to refer back to the value in the table and thats the weird part because the value is the button right if the tag is linked to the textbutton or am I wrong?

@enpiesie

This is how it looks like in studio right now I made a new project to try and find out whats going wrong inside of this script it loops 2 times since there are 2 buttons but if I press any of them while playing nothing happens a really annoying thing I dont know how to solve or how to even try it

dang it I think I know what the problem is this kind of script doesnt work for screen gui buttons bc they are used by a local script only a I cant use a local script for this I tried the exact same code only with the Touched event of a workspace part instead of the buttons and it worked fine if im mistaken someone please explain it to me

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