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

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

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?

2 comments

0

Is the script in the button a local script or regular script?

0

You say the script works, but it's the problem?

1 answer

✓ Accepted answer

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)

1 comment

0

Ohh I never thought of that, thanks a lot!

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