How do I make my tycoon button play a sound when purchased?

  1. What do you want to achieve? I want to make it so that when a button in my tycoon is stepped on, it plays a sound.

  2. What is the issue? My current solution produces 0 errors but doesn’t work at all.

  3. What solutions have you tried so far? I tried firing an event on the client but it didnt play the sound.

button.PrimaryPart.Touched:Connect(function(hit)
				local playButtonSoundEvent = replicatedStorage:FindFirstChild("playButtonSoundEvent")
				local buttonPurchaseSound = replicatedStorage:FindFirstChild("buttonPurchaseSound")
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if not player then return end
				if plot:GetAttribute("Owner") ~= player.UserId then return end
				-- TO DO LIST!!! :)
				-- Remove currency
				-- Check if they have enough
				-- Give em the item
				-- Code below here that runs means what touched it was plot owner!
				-- THE END!!! :(
				local itemToBuyId = button:GetAttribute("ITEMTOBUYID")
				if not itemToBuyId then warn("goofy man you forget to add an id") return end
				
				for _, item in templateItems:GetChildren() do
					if item:GetAttribute("ID") == not itemToBuyId then continue end
						-- Correct item found! Wahoo!
						-- Clone item and reposition it.
						local itemClone = item:Clone()
						itemClone.Parent = itemsFolder
						
						local itemCFrame
						if itemClone:IsA("Model") then -- What kind of item is the itemClone?
							itemCFrame = itemClone:GetPivot() -- Models have no CFrame <:(
						elseif itemClone:IsA("BasePart") then
							itemCFrame = itemClone.CFrame
						end
						
						local relativeItemCFrame = templatePlot.CFrame:ToObjectSpace(itemCFrame)
						local worldCFrameOfNewPlot = plot.CFrame:ToWorldSpace(relativeItemCFrame)
						if itemClone:IsA("Model") then
						itemClone:PivotTo(worldCFrameOfNewPlot)
						elseif itemClone:IsA("BasePart") then
						itemClone.CFrame = worldCFrameOfNewPlot
						end
						playButtonSoundEvent:FireClient(player, buttonPurchaseSound)
						button:Destroy()
					end
				end)

This is the event script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local playButtonSoundEvent = replicatedStorage:WaitForChild("playButtonSoundEvent")
local buttonPurchaseSound = replicatedStorage:WaitForChild("buttonPurchaseSound")

playButtonSoundEvent.OnServerEvent:Connect(function(player, buttonPurchaseSound)
	buttonPurchaseSound:Play()
	print("yes!")
end)

This is what happens:

If you know how I can make this work please please reply down below I have tried fixing this for so long but it’s so hard :frowning:.

1 Like

That’s because buttonPurchaseSound is still in ReplicatedStorage, meaning when you play it, the player won’t hear the sound. Try cloning buttonPurchaseSound into the character, then play then sound:

local newButtonPurchaseSound = buttonPurchaseSound:Clone()
newButtonPurchaseSound.Parent = player.Character
newButtonPurchaseSound:Play()


-- destroy the sound after use

local debris = game:GetService("Debris")
debris:AddItem(newButtonPurchaseSound, newButtonPurchaseSound.TimeLength)
4 Likes

am i doing it wrong because it doesnt work

local replicatedStorage = game:GetService("ReplicatedStorage")
local playButtonSoundEvent = replicatedStorage:WaitForChild("playButtonSoundEvent")
local buttonPurchaseSound = replicatedStorage:WaitForChild("buttonPurchaseSound")
local LocalPlayer=game:GetService("Players").LocalPlayer

playButtonSoundEvent.OnServerEvent:Connect(function()
--No need to send the sound, it's already defined here
--Player isn't a parameter, it only defines which client to send to
  --parent sound item to player
  local newButtonPurchaseSound = buttonPurchaseSound:Clone()
  newButtonPurchaseSound.Parent = LocalPlayer.Character.HumanoidRootPart
  newButtonPurchaseSound:Play()
  print("yes!")
  --destroy the sound after it finishes
  local debris = game:GetService("Debris")
  debris:AddItem(newButtonPurchaseSound, newButtonPurchaseSound.TimeLength)
end)

This is what the full client side code should look like. With this, change the FireClient line:

playButtonSoundEvent:FireClient(player)

I tried parenting it under ServerScriptService AND StarterPlayerScripts: SSS has no errors but doesnt work, and SPS says that OnServerEvent can only be used on the server

Should it not be parented under soundservice?

With sound service, we get no errors but it doesn’t work, similar to the issue with SSS

Sorry, I messed up. The OnServerEvent should be OnClientEvent. It should be in StarterPlayerScripts.

2 Likes

Thank you SO MUCH FOR THE HELP ITS FINALLY WORKING!

1 Like

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