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()
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)
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?
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)
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!