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

How to pick a random Sound when clicked (GUI)?

I have a shop, and i want when someone buys something or close the shop or open the shop, etc. A random sound will be chosen Example: Close shop = GoodbyeSound, SeeYouLaterSound, SeeyounextimeSound, etc.

Here is my script just in case:

local open = game.Workspace.openPart

local close = game.Workspace.closePart

local frame = script.Parent

local closeButton = frame.closeButton

local buy_1 = frame.buy1

local buy_2 = frame.buy2

local ting = 0



local ReplicatedStorage = game:GetService('ReplicatedStorage')

local remoteEvent = ReplicatedStorage:WaitForChild('BuyTool')



local LPlayer = game.Players.LocalPlayer  



frame.Visible = false



local function shopMenu(otherPart)
	if ting == 0 then
		ting = 1

	local player = game.Players:FindFirstChild(otherPart.Parent.Name)

	if player == LPlayer then  

		player.PlayerGui.ScreenGui.Shop.Visible = true
		
		script.Parent.Welcome:Play()
		
		player.Character.Humanoid.WalkSpeed = 0

	end
		wait(1) 
		ting = 0
	end
end



local function closeMenu()  --Updated to use LPlayer
	if ting == 0 then
		ting = 1

	    LPlayer.PlayerGui.ScreenGui.Shop.Visible = false
	
	    script.Parent.Goodbye:Play()

	    LPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(close.Position.X,close.Position.Y + 3,close.Position.Z)

		LPlayer.Character.Humanoid.WalkSpeed = 16
		wait(1) 
		ting = 0
	end
end



local function buyTool1()

	local tool = ReplicatedStorage.ShopItems.BloxyCola

	remoteEvent:FireServer(tool)

end



local function buyTool2()

	local tool = ReplicatedStorage.ShopItems.Coke

	remoteEvent:FireServer(tool)

end


open.Touched:Connect(shopMenu)

closeButton.MouseButton1Click:Connect(closeMenu)

buy_1.MouseButton1Click:Connect(buyTool1)

buy_2.MouseButton1Click:Connect(buyTool2)

1 answer

Make an array for your sound name/id.

local sounds = {
    "Your sound name/id",
    "Your sound name/id",
    "Your sound name/id"
}

Then use math.random function.

Example if you are using sound name:

local chosenSound = sounds[math.random(1, #sounds)]

local sound = script.Parent:WaitForChild(chosenSound) -- define your sound
sound:Play()

Example if you are using sound id:

local chosenSound = sounds[math.random(1, #sounds)]

local sound = -- define your sound instance
sound.SoundId = "rbxassetid://" ..tonumber(chosenSound)
sound:Play()

4 comments

0

Where can i paste this? Because its bugging my shop

0

I mean, in which line

0

at the top a guess

0

Paste it in your inside closeMenu function line 57

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