Sound player keeps trying to give me sound

So I have this “step on sound” player, and it works perfectly fine, but it keeps giving or atleast trying to give me sound after I already got it, and it spams my output, any help on capping this after I got one?

SERVER:

local Button = script.Parent
local Sound = Button:WaitForChild("Sound")
local SoundPlayer = script:WaitForChild("SoundPlayer")

function GiveSound(Character)
	local Player = game.Players:GetPlayerFromCharacter(Character)
	if Player ~= nil then
		local PlayerGui = Player:FindFirstChild("PlayerGui")
		if PlayerGui ~= nil then
			local SoundScript = SoundPlayer:clone()
			local SoundCopy = Sound:clone()
			SoundCopy.Name = "Sound"
			SoundCopy.Parent = SoundScript
			
			SoundScript.Parent = PlayerGui
			SoundScript.Disabled = false
			end
		end
	end

function Stepped(Part)
	if Part.Parent == nil then return end
	GiveSound(Part.Parent)
end
Button.Touched:connect(Stepped)

LOCAL (Child of server)

local Camera = game.Workspace.CurrentCamera
local Sound = script:WaitForChild("Sound")

local RobloxHasWorkingIsPlaying = false
	local SoundDeleteTimeForNonLooped = 1
function PlaySound()
	Sound.Name = "SteppedSound"
	Sound.Parent = Camera
	if RobloxHasWorkingIsPlaying then
		Sound.Changed:connect(function(Property) if Property == "IsPlaying" and Sound.IsPlaying == false then Sound:Destroy(); script:Destroy() end end)
		wait (1)
		Sound:Play()
	elseif not Sound.Looped then
		game.Debris:AddItem(Sound,SoundDeleteTimeForNonLooped)
		wait (1)
		Sound:Play()
		script:Destroy()
	else
		Sound:Play()
		script:Destroy()
	end
end

function Soundify()
	local OldSound = Camera:FindFirstChild("SteppedSound")
	if OldSound ~= nil then
		if OldSound.SoundId ~= Sound.SoundId then
			OldSound:Destroy()
			PlaySound()
		else
			script:Destroy()
		end
	else
		PlaySound()
	end
end

Soundify()
2 Likes

why bother creating a sound on the server?

1 Like

Because I have multiple of these and I need to be able to delete the old one to replace with the new one, (indoor music system)

i suggest you do things like sounds clientside, since there is absolutely no benefit for the server to know if a sound exists or not (if you get how i mean that)

1 Like

But if im going to be deleting the old one, the server needs to see it to delete it.

the server will not have a copy to begin with, call it with remote events/functions

How would I rewrite this to make this 1. Not “try” to give me more sound’s and spam my output, 2. Be able to change songs when I step on a new one, and 3. And make it how you said?

first of all, my own approach would be to insert either some sort of value that the client reads, so it changes songs accordingly

or you keep track of each players’ HumanoidRootPart (if provided) and just send out a :FireClient(player) with the according settings

So this is what i have so far, each of the values is bool. image

Heres a local script I put into the StarterGui. How would I make it so once the player hits a child of “SoundTouch” depending on the value of the Playing (which I am going to change to string value instead of bool, it will play that.

local Songs = {
    [1] = "rbxassetid://279269808", -- City Ambient
    [2] = "rbxassetid://1346697294", -- ZEZE
	[3] = "rbxassetid://415294592", -- Nobody Can Hold Me
}

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:Connect(function(character)
    
	end)

I am not sure if I should grab the player as local player = game.Players.LocalPlayer or using the PlayerAdded function?

I still have 0 clue on where to go from here, anyone help me?

In the server script, you could try disconnecting the Touched event. Try using this code for your stepped function and the connection:

function Stepped(Part)
	if Part.Parent == nil then return end
	GiveSound(Part.Parent)
    connection:Disconnect()
end
connection = Button.Touched:connect(Stepped)

This just assigns the connection to a variable, and in the function it is being connected to, disconnects it afterward. This should make the code only run once. Hope I helped!