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

Why is nothing happening when player from certain team dies?

I have this script to make a print statement when a player from the Bright red team dies, but nothing happens when they die. Why?

game.Players.PlayerAdded:connect(function(Player) 
	Player.CharacterAdded:connect(function(Character)
	if Player.TeamColor == BrickColor.new("Bright red") then
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid.Died:connect(function(Died)
	print('One player has died')
	end) 
	end		
	end)
end)

5 comments

0

If you are testing this offline then your player is created before scripts run, so PlayerAdded and CharacterAdded are never fired.

0

I am trying this online.

0

You're code is hard to read, please tab correctly. =)

0

Try adding print(Player.TeamColor) before line 3.

0

@ NathanTheDeveloper I apologize, I think you or someone else once told me before I should be tabbing more. I usually do it at the end to make it look neat, I just completely forgot to do it here. @ 1waffle1 I will try that and see if the script is running correctly

1 answer

✓ Accepted answer

Notice how you put the function in the game.Players.PlayerAdded and the Player.CharacterAdded... Well, just know that these will not always run, so what I would do is create a Script in StarterPack. The reason why I would do this is because you can't always define the player in Scripts that are not a descendant or a parent of the player. So in this script in StarterPack, try doing this:

local player = script.Parent.Parent
repeat wait() until player.Character ~= nil
local character = player.Character
local humanoid = character.Humanoid

humanoid.Died:connect(function()
	if player.TeamColor = BrickColor.new('Bright red') then
		print('A player has died!')
	end
end

So, first we define the player. This has to be script.Parent.Parent, since the script is going to be inserted into Backpack, and Backpack is the parent of the script, and then the Player is the parent of the Backpack.

Then we wait until the character exists. Better to be safe than sorry!

Then we define more variables.

Next, we make the humanoid.Died function. Then we check if the player is in the same team, and if it is, then it will print when they die.

I hope this helped!

3 comments

0

EDIT: Don't put it in StarterPack, put it in StarterGui.

0

You forgot end) on the tenth line and I had to add 'Died' without '' into function(), like so function(Died) and it made it work! I would like to keep it in the StarterPack over the StarterGui as its one less variable for me as I have other scripts that are constantly moving around things in StarterGui. Unless it is bad practice then I will move it. Thanks so much you don't know how much you've he

0

No worries.

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