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

How would i destroy the sound from playing once its already played?

local sound = "rbxassetid://138088421"

script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(hit.Parent) game.ReplicatedStorage.PlayMusic:FireClient(player,sound) end end)

How would i destroy the sound so it doesn't play again when a player walks into the brick?

1 answer

✓ Accepted answer

It looks like you're using remote events, but what you've provided is only the part where the server fires the client. What's not provided is how the client responds to that fire, but I would assume that a LocalScript in your game :Play()s the audio.

My best guess is that you're having the client play the audio so only they hear it and none of the other players, and that you're running into issues because the sound is playing repeatedly as they touch the part?

I tried my best to replicate what I think you're doing -

In a part in the workspace:

local sound = "rbxassetid://138088421"

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		game.ReplicatedStorage.PlayMusic:FireClient(player,sound)
	end
end)

In a local script in the player:

function playMusic(id)
	workspace.Sound.SoundId = id -- not sure where your sound object is, but it seems that its id is subject to change?
	workspace.Sound:Play();
end

game:GetService("ReplicatedStorage").PlayMusic.OnClientEvent:Connect(playMusic)

This seemed to work, and I was able to replicate your problem (the sound effect playing multiple times)

Since you're having the server tell the client when to play, I can understand that you probably wouldn't want the part to only play it once, since other players wouldn't be able to hear it. I'd recommend just letting the part remember who has and has not heard the effect.

Solution

In the part:

local heard = {} -- add usernames into this table

local sound = "rbxassetid://138088421"

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local alreadyHeard = false -- by default assume they haven't touched it
		for i,v in pairs(heard) do -- check it against everyone in the list
			if hit.Parent.Name == v then -- found a match
				alreadyHeard = true -- they already heard it
			end
		end
		if not alreadyHeard then -- true by default
			table.insert(heard,#heard+1,hit.Parent.Name) -- add their username to the list
			-- then proceed to play the sound
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			game.ReplicatedStorage.PlayMusic:FireClient(player,sound)
		end
	end
end)

I hope this helped! If it did, please mark it as the solution or upvote it. If you have any questions, please comment below :)

1 comment

0

Thanks so much!

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