So look, in my obby, I already used the 4 colored spawns ROBLOX made for us. Then I had to make my own spawns, by cloning one of them, and putting a script in it so you change teams when you touch one of my custom spawns.
But the problem is with my custom spawns. If you die, you don't spawn on the custom spawns, you just spawn in the middle of nowhere.
That is why I created this script:
game.Players.PlayerAdded:connect(function(plr) plr.Character.Humanoid.Died:connect(function(died) if plr.TeamColor == BrickColor.new("New Yeller") then wait(6) died.Parent:MoveTo(game.Workspace.Custom1.Position) elseif plr.TeamColor == BrickColor.new("Toothpaste") then wait(6) died.Parent:MoveTo(game.Workspace.SpecialSpawn2.Position) elseif plr.TeamColor == BrickColor.new("Pink") then wait(6) died.Parent:MoveTo(game.Workspace.Custom3.Position) else print("The player that died isn't on any of the custom spawn teams, so they weren't teleported") end end) end)
It has an error. In output, it is saying this: 11:38:21.241 - Workspace.Script:2: attempt to index field 'Character' (a nil value) Help?
The Humanoid.Died event doesn't have any parameters, so the "died" variable is nil. You should be using the CharacterAdded event so that it hooks up to the death event each time the player respawns.
game.Players.PlayerAdded:connect(function(plr) plr.CharacterAdded:connect(function(char) char:WaitForChild("Humanoid").Died:connect(function(died) if plr.TeamColor == BrickColor.new("New Yeller") then wait(6) died.Parent:MoveTo(game.Workspace.Custom1.Position) elseif plr.TeamColor == BrickColor.new("Toothpaste") then wait(6) died.Parent:MoveTo(game.Workspace.SpecialSpawn2.Position) elseif plr.TeamColor == BrickColor.new("Pink") then wait(6) died.Parent:MoveTo(game.Workspace.Custom3.Position) else print("The player that died isn't on any of the custom spawn teams, so they weren't teleported") end end) end) end)
One possibility is the fact the script is running before the character loads. Try adding this above line 2, and tell me what happens.
repeat wait() until plr.Character
This should hopefully resolve that error.
EDIT:
After looking at the wiki, which you can see here, we can disregard my previous answer, and incorporate their example:
game:GetService('Players').PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() print(player.Name .. " has died!") end) end) end)
Try using their example, and editing the main inside the Died event.