Sound playing for everyone

I have this script, but when “in game” I activate the script it plays the sound for evryone in the server, and I want to fix that.

local ProximityPrompt = script.Parent:WaitForChild("ProximityPrompt")
local Sound = script:WaitForChild("Sound")

ProximityPrompt.Triggered:Connect(function(playerWhoTriggered)
	local Character = playerWhoTriggered.Character or playerWhoTriggered.CharacterAdded:Wait()
	
	if not Character or not Character:FindFirstChild("Humanoid") then
		warn("Character or Humanoid not found for player: " .. playerWhoTriggered.Name)
		return -- Exit if character or humanoid is not available
	end

	Character.Humanoid:UnequipTools()

	local backpack = playerWhoTriggered:FindFirstChild("Backpack")
	if not backpack then
		warn("Backpack not found for player: " .. playerWhoTriggered.Name)
		return
	end

	if backpack:FindFirstChild("Empty Box") then
		local leaderstats = playerWhoTriggered:FindFirstChild("leaderstats")
		if leaderstats then
			local coins = leaderstats:FindFirstChild("Coins")
			if coins then
				coins.Value = coins.Value + 3
				Sound:Play()
				local kickball = backpack:FindFirstChild("Empty Box")
				if kickball then
					kickball:Destroy()
				end
				ProximityPrompt.ActionText = "Item sold!"
				task.wait(2)
				ProximityPrompt.ActionText = "Sell items"
			else
				warn("Coins stat not found for player: " .. playerWhoTriggered.Name .. ". Cannot add coins.")
				ProximityPrompt.ActionText = "Error: Coins stat missing!"
				task.wait(2)
				ProximityPrompt.ActionText = "Sell items"
			end
		else
			warn("Leaderstats not found for player: " .. playerWhoTriggered.Name .. ". Cannot add coins.")
			ProximityPrompt.ActionText = "Error: Leaderstats missing!"
			task.wait(2)
			ProximityPrompt.ActionText = "Sell items"
		end
	else
		ProximityPrompt.ActionText = "Give me something you broke loser!"
		task.wait(2)
		ProximityPrompt.ActionText = "Sell items"
	end
end)

sir could you be specific on what sound your talking about like sound for shooting or picking up coins and can you also tell us what script you wrote this code in

The sound needs to play on the client. Currently, the sound is inside the Part with the prompt in it. When it plays from a Script (which runs on the server), that sound is replicated to each person by the server. If you play a sound from a LocalScript, only the client will play the sound (and this does not replicate to the server, and then to the other clients)!

Here’s how to fix it:

Add a RemoteEvent under ReplicatedStorage called “CoinSound”

Make a local script in StarterPlayer, and insert the following text:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.CoinSound.OnClientEvent(function(part: BasePart)
    if part:FindFirstChildOfClass("Sound") then
          part:FindFirstChildOfClass("Sound"):Play()
    end
end)

In the server script (inside the part), replace
Sound:Play()
with
game.ReplicatedStorage.CoinSound:FireClient(playerWhoTriggered)

Good luck!

4 Likes

@notsfeelings is right about the fact that the sound is in the part but just do this instead because it’s easier

all you need to do is put the sound in ReplicatedStorage

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ProximityPrompt = script.Parent:WaitForChild("ProximityPrompt")
local Sound = ReplicatedStorage.Sound

ProximityPrompt.Triggered:Connect(function(playerWhoTriggered)
	local Character = playerWhoTriggered.Character or playerWhoTriggered.CharacterAdded:Wait()
	
	if not Character or not Character:FindFirstChild("Humanoid") then
		warn("Character or Humanoid not found for player: " .. playerWhoTriggered.Name)
		return -- Exit if character or humanoid is not available
	end

	Character.Humanoid:UnequipTools()

	local backpack = playerWhoTriggered:FindFirstChild("Backpack")
	if not backpack then
		warn("Backpack not found for player: " .. playerWhoTriggered.Name)
		return
	end

	if backpack:FindFirstChild("Empty Box") then
		local leaderstats = playerWhoTriggered:FindFirstChild("leaderstats")
		if leaderstats then
			local coins = leaderstats:FindFirstChild("Coins")
			if coins then
				coins.Value = coins.Value + 3
				Sound:Play()
				local kickball = backpack:FindFirstChild("Empty Box")
				if kickball then
					kickball:Destroy()
				end
				ProximityPrompt.ActionText = "Item sold!"
				task.wait(2)
				ProximityPrompt.ActionText = "Sell items"
			else
				warn("Coins stat not found for player: " .. playerWhoTriggered.Name .. ". Cannot add coins.")
				ProximityPrompt.ActionText = "Error: Coins stat missing!"
				task.wait(2)
				ProximityPrompt.ActionText = "Sell items"
			end
		else
			warn("Leaderstats not found for player: " .. playerWhoTriggered.Name .. ". Cannot add coins.")
			ProximityPrompt.ActionText = "Error: Leaderstats missing!"
			task.wait(2)
			ProximityPrompt.ActionText = "Sell items"
		end
	else
		ProximityPrompt.ActionText = "Give me something you broke loser!"
		task.wait(2)
		ProximityPrompt.ActionText = "Sell items"
	end
end)
1 Like