Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

0

Is this in a Local Script or a Script?

1 answer

✓ Accepted answer

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 comment

1

Thanks! It now works!

This discussion is preserved as it was. New answers, comments, and votes are closed.