How can i make the player teleport to a part when he dies?s

i have a player spawner that spawns the player when he joins, but i want when the player dies he teleports to a part named “location” ONLY when he dies, but the problem that the player teleports to the player spawner instead not to the location part

how can i do this ??
i made a script that checks for CharacterAdded but that also dont work it teleports to the player spawner not to the location part

4 Likes
local players = game:GetService("Players")

local location = -- Path to the location part

local function playerAdded(player)
    local function died()
        if not player:GetAttribute("HasDied") then
            player:SetAttribute("HasDied", true)
        end
    end

    local function characterAdded(char)
        if player:GetAttribute("HasDied") then
            char:PivotTo(location.CFrame)
        end

        char:WaitForChild("Humanoid").Died:Once(died)
    end

    player.CharacterAdded:Connect(characterAdded)
end


players.PlayerAdded:Connect(playerAdded)
3 Likes

Why functions inside functions?

2 Likes

Can’t access the Player object outside of the function.

And don’t tell me to do this. This is BS.

player.CharacterAdded:Connect(function(char)
    characterAdded(char, player)
end)
2 Likes

how is it bs

Create a function just to call another function? Please. Not to mention it’s bad practice.

It’s like doing this.

local function addToValue(value)
    value.Value += 1
end

local function callAddToValue(value)
    addToValue(value)
end
local justJoined = {}

game.Players.PlayerAdded:Connect(function(player)
   table.insert(justJoined, player)
   player.CharacterAdded:Connect(function(character)
      if table.find(justJoined, player) then
         table[player] = nil
      else
         character.HumanoidRootPart.CFrame = workspace.location.CFrame
      end
   end)
end)