Issue In-game and not in studio

There’s an issue with me teleporting to the correct stages when navigating through stages and when joining the game, thought it works perfectly fine in studio. I’ve tested multiple times to see if this was just one case, however it seems to be happening all the time. This is the following error I get on the server:

This is the script that is getting errored:

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("data_01")

local camera = workspace.CurrentCamera

local cpFolder = game.Workspace:WaitForChild("Checkpoints")


game.Players.PlayerAdded:Connect(function(player)
	local key = "player-" .. player.UserId

	local GetSave = DataStore:GetAsync(key) -- check for existing data 

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local hidden = Instance.new("Folder", player)
	hidden.Name = "hidden"

	local checkpoint = Instance.new("IntValue", leaderstats)
	checkpoint.Name = "Stage"

	local spawnpoint = Instance.new("IntValue", hidden)
	spawnpoint.Name = "SpawnPoint"

	if GetSave then
		checkpoint.Value = GetSave
		print("Data Loaded For " .. player.Name)
	else
		checkpoint.Value = 1
		print("New Data Created For " .. player.Name)
	end
		
	local Character = player.Character

	local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
	Character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)

	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[player.hidden.SpawnPoint.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	end)

end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = "player-" .. player.UserId
	
	DataStore:SetAsync(key, player.leaderstats.Stage.Value)
	
	print("Data Successfully Saved For " .. player.Name)
end)

For reference, the error is on this line: Character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)

The error means Character is nil.

To fix this you would just do local Character = player.Character or player.CharacterAddded:Wait() This line sets the variable to the player’s character if it already exists or waits for the player’s character to be added and set it as the variable.

2 Likes

I tried the second method, and it works fine in-game and now not in studio but this time its a different issue in studio.

Are you receiving any errors in the Output window?

Nope, none. I did at first but I can’t repro it, It just won’t teleport me to the correct stage when I test in studio.

Can you try adding print statements to see where the script is stopping?

Nothing is broken, all the lines are being reached just when I test in studio it should teleport me to the stage instead of at the lobby which is what’s happening in game and not in studio.

Yeah I think it’s not erroring because it’s doing what it’s suppose to do but not in the sense that it’s working for my use-case. I’m trying to make it so that the player spawns at their current stage which isn’t what’s happening for some reason in studio, which would be the line that was erroring before in-game.