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

Help with CharacterAdded()?

repstorage.RoundTag.Changed:connect(function(value)
	if value == true then
		print("1")
		if repstorage.GameRound.Value == "Deathmatch" then
			print("2")
			for _, player in pairs(game.Players:GetPlayers()) do
				print("3")
				player.CharacterAdded:connect(function(character)
					print("4")
					character:WaitForChild("Humanoid").Died:connect(function()
						print("5")
						wait()
						if character.Humanoid:FindFirstChild("creator") then
							print("6")
							local tag = character.Humanoid.creator
							local plr = tag.Value
						    local tColor = player.TeamColor
						    local kColor = tag.Value.TeamColor
						   	if not kColor then return end
						    if not tColor then return end    
							if tColor ~= kColor then
						   		if kColor == BrickColor.new("Lime green") then
						        	greenscore.Value = greenscore.Value + 1
						        elseif kColor == BrickColor.new("New Yeller") then
						        	yellowscore.Value = yellowscore.Value + 1
						   		end
							end
						end
					end)
				end)
			end
		end
	end
end)

Ok, so this works but, for it to work you must: Kill someone first, then kill them AGAIN to get the score + 1. How come it takes 2 deaths for the score to start working. I want so if you kill them it gives you a score straight away. Please help.

EDIT

Output 1 2 3 3 4 Prints 1 2 3 3 when game starts and 4 when the player dies

When you kill a player again then it prints 5 and 6

3 comments

0

I figured out the problem, sorry it took so long.

0

please read my comment there is an error on line 7 of your code with the player

0

Try it now.

1 answer

✓ Accepted answer

Figured out the problem!

What's happening here is you're waiting for the CharacterAdded event to fire for Players that already have Characters!

Sorry that took so long.

To fix this, I'm making the CharacterAdded code a function, to avoid typing it twice, and doing this:

function listenForDeath(player)
	local character = player.Character
	character:WaitForChild("Humanoid").Died:connect(function()
		wait()
		if character.Humanoid:FindFirstChild("creator") then
			local tag = character.Humanoid.creator
			local plr = tag.Value
			local tColor = player.TeamColor
			local kColor = tag.Value.TeamColor
			if not kColor then return end
			if not tColor then return end    
			if tColor ~= kColor then
				if kColor == BrickColor.new("Lime green") then
					greenscore.Value = greenscore.Value + 1
				elseif kColor == BrickColor.new("New Yeller") then
					yellowscore.Value = yellowscore.Value + 1
				end
			end
		end
	end)
end

repstorage.RoundTag.Changed:connect(function(value)
    if value == true then
        if repstorage.GameRound.Value == "Deathmatch" then
            for _, player in pairs(game.Players:GetPlayers()) do
                player.CharacterAdded:connect(function(character)
					listenForDeath(player)
                end)
				if player.Character then
					listenForDeath(player)
				end
            end
        end
    end
end)

10 comments

0

Ill post the whole script, its not too long but can you have a quick read through? Question editted

0

I still don't see any errors. Did you try adding the wait()? That's really all I can think of.

0

yeah, i added it to line "6" but still not working, it dosent work until you kill a person again

0

wait, ill add some prints and see what it prints when I kill someone for the first time

0

Editted answer with prints and output from the prints

0

on line 12 it has an underline under player

0

yeah, when I play it has an error for line 7 I mean that player hasnt been indicated or something

0

Oops, let me fix that real quick...

0

OMG THANKS :D It works, thank so much :D

0

Hey, I have a problem with this it fully works but rarely I get when you kill someone it gives +2 points and not +1, is there a wait() or something I should add?

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