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

In my obby game, how the I fix the nil error?

Asked by 2 years ago

So, I'm creating an obby game and I created a stage spawner. The new stage number in the leaderstats does work while spawning on the stage spawner after the player dies doesn't work.

The error is saying, "attempt to index nil with UpperTorso"

Here's the script I put in:

game.Players.ChildAdded:Connect(function(player)

    local stagevalue = player.leaderstats.Stage.Value

    local stagespawn = workspace.Stages[stagevalue]

    local torso = player.Character.UpperTorso

    torso.CFrame.New = stagespawn.CFrame.Value

end)
0
Is this in a Local Script or a Script? notZipZen 0 — 2y

1 answer

Log in to vote
1
Answered by
Miniller 562 Moderation Voter
2 years ago

There is a reason why there are StarterPlayerScripts and StarterCharacterScripts in your Roblox Studio. It takes a few seconds to load the character after the player has joined. The more assets (models, parts, etc) you have in your game, the longer it will take for everything to load, especially if the assets are not cached (players joining for the first time) You can use the player.CharacterAdded event to wait for the character to spawn. Also use PlayerAdded instead of ChildAdded

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        print("Character added")
    end)
end)

I'm not sure why but the actual teleport didn't work for me. But you shouldn't be doing this either way, you should use SpawnLocations and Teams for this.

You can also just save the last touched spawnlocation's position. Check out the Obby template in Roblox Studio. It saves the positions but to make it work you need to save it to a DataStore and also read it when a player joins.(credits to the deleted user commenting under this post) Just use a DataStore, there are great tutorials out there. This one is from AlvinBlox

1
Thanks! It now works! ieverhart 62 — 2y
Ad

Answer this question