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

Help with CharacterAdded()?

Asked by 9 years ago
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

0
I figured out the problem, sorry it took so long. adark 5487 — 9y
0
please read my comment there is an error on line 7 of your code with the player NinjoOnline 1146 — 9y
0
Try it now. adark 5487 — 9y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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)
0
Ill post the whole script, its not too long but can you have a quick read through? Question editted NinjoOnline 1146 — 9y
0
I still don't see any errors. Did you try adding the `wait()`? That's really all I can think of. adark 5487 — 9y
0
yeah, i added it to line "6" but still not working, it dosent work until you kill a person again NinjoOnline 1146 — 9y
0
wait, ill add some prints and see what it prints when I kill someone for the first time NinjoOnline 1146 — 9y
View all comments (6 more)
0
Editted answer with prints and output from the prints NinjoOnline 1146 — 9y
0
on line 12 it has an underline under player NinjoOnline 1146 — 9y
0
yeah, when I play it has an error for line 7 I mean that player hasnt been indicated or something NinjoOnline 1146 — 9y
0
Oops, let me fix that real quick... adark 5487 — 9y
0
OMG THANKS :D It works, thank so much :D NinjoOnline 1146 — 9y
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? NinjoOnline 1146 — 9y
Ad

Answer this question