Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

This script doesn't work after I re-enable it using another script, why?

Asked by
Kegani 31
6 years ago
Edited 6 years ago

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?

0
Is the script in the button a local script or regular script? hellmatic 1523 — 6y
0
You say the script works, but it's the problem? hellmatic 1523 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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)
0
Ohh I never thought of that, thanks a lot! Kegani 31 — 6y
Ad

Answer this question