0
\$\begingroup\$

So it works first time when the player dies. But when it dies twice the for loop doesn't work.

if winner == "Team DeathMatch" then
    local player = game:GetService("Players").LocalPlayer
    local allplrs = game:GetService("Players"):GetPlayers()
    for _,v in pairs(allplrs) do
        local h = v.Character:FindFirstChild("Humanoid")
        h.Died:Connect(function()
            local red_cf = CFrame.new(28.859, 8.588, 104.769)
            local blue_cf = CFrame.new(-148.635, 8.115, 104.769)
            warn("someone ded")
            local function o()
                return v.team.Value
            end
            warn("work here i guesss")
            local x = o()
            if x == "blue" then

                warn("hi")

                v:LoadCharacter()

                v.Character:SetPrimaryPartCFrame(blue_cf)
                game.ReplicatedStorage.remotes.tdstuff:FireClient(v)
            elseif  x == "red" then

                warn("hi")

                v:LoadCharacter()

                v.Character:SetPrimaryPartCFrame(red_cf)
                game.ReplicatedStorage.remotes.tdstuff:FireClient(v)

            end     
        end)
    end
    wait(150)
    for i,plr in pairs(game.Players:GetPlayers()) do
        plr:LoadCharacter()
    end
end
```
\$\endgroup\$
1
  • 4
    \$\begingroup\$ "Doesn't work" is usually not enough information to troubleshoot a problem in game development. Everything from "the character moves 1 mm less than I wanted" to "my computer catches fire" qualifies as "doesn't work" - so we need to be more specific. What should this code do when it's working correctly? What specific outcomes do you observe instead? Clearly identifying the symptoms helps users quickly and accurately identify the cause. \$\endgroup\$ Commented Aug 11, 2021 at 11:44

1 Answer 1

2
\$\begingroup\$

Looks like this code runs after voting on a game mode. That probably means it doesn't run again until the match is over. My guess is that game:GetService("Players"):GetPlayers() gives you a list of players that exist throughout the match, but their humanoid (v.Character:FindFirstChild("Humanoid") only exists until they die.

If that's correct, then h.Died:Connect(function() ... only sets the Died callback for the character that each player starts with. So when players get a new character (after they die and respawn), the Died callback isn't hooked up again.

You'll have to look for something like a Spawn callback on the player (v) and setup the Died callback there instead.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.