Hello
I made a script that when a player dies, a sound plays.
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character.Humanoid.Died:connect(function(dead) game.Workspace.assets.cannon:Play() end) end) end)
It works, and after further research, I think this script is the problem.
I use a GUI to disable that script and re-enable, and it works, however, when I re-enable it, in Properties it is indeed enabled but doesn't work.
By the way, the button that disables the script has this script in it:
function onClick() game.Workspace.cannon.Disabled = true end script.Parent.MouseButton1Click:connect(onClick)
To enable it I just change true to false.
What is happening and what can I do?
The problem is that the function in your script triggers when a player joins the game
game.Players.PlayerAdded:connect(function(player)
to fix this you can use a for loop with ipairs to create the function for the players that are already in the game like so
function DeathSound(player)--main function that plays sound when triggered if player.Character then--if their character is already loaded then wait for them to die player.Character.Humanoid.Died:connect(function() game.Workspace.assets.cannon:Play() end) end player.CharacterAdded:connect(function(character)--when chrctr loads wait for thm 2 die character.Humanoid.Died:connect(function() game.Workspace.assets.cannon:Play() end) end) end for i,v in ipairs(game.Players:GetChildren()) do if v.ClassName == "Player" then DeathSound(v)--triggers main function for all players already in the game end end game.Players.PlayerAdded:connect(function(player) DeathSound(player)--triggers the main function for players when they first join end)