How do I make this so the players don't spawn in the air?

I am making an obby as a side project, and I want to know how I can fix this script so the players stop spawning in the air when they first join the game.

Game Handler

local DataStoreService = game:GetService("DataStoreService")
local levelDataStore = DataStoreService:GetDataStore("levelstwobeta")
local coinsDataStore = DataStoreService:GetDataStore("coinstwobeta")
local promocodeastore = DataStoreService:GetDataStore("promocodeA")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Checkpoint = Instance.new("IntValue", leaderstats)
	Checkpoint.Name = "Level"
	
	local Coins = Instance.new("IntValue", leaderstats)
	Coins.Name = "Coins"
	
	local PromoCodeA = Instance.new("IntValue", player)
	PromoCodeA.Name = "JustReleased"
	PromoCodeA.Value = 0
	
	local data
	local dataII
	local dataIII
	local success, errormessage = pcall(function()
		data = levelDataStore:GetAsync(player.UserId.."-level")
		dataII = coinsDataStore:GetAsync(player.UserId.."-coins")
		dataIII = promocodeastore:GetAsync(player.UserId.."-promocodea")
	end)
	
	if success then 
		Checkpoint.Value = data
		Coins.Value = dataII
		PromoCodeA.Value = dataIII
	else
		warn(errormessage)
	end
	
	if Checkpoint.Value == 0 then
		Checkpoint.Value = 1
	end
	
	player.CharacterAdded:Connect(function(character)
		repeat wait() until player.character ~= nil
		local checkpoint = workspace.Checkpoints:FindFirstChild(Checkpoint.Value)
		character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 2, 0))
	end)
end)
	
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		levelDataStore:SetAsync(player.UserId.."-level", player.leaderstats.Level.Value)
		coinsDataStore:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
		promocodeastore:SetAsync(player.UserId.."-promocodea", player.JustReleased.Value)
	end)
		
	if success then
		print("The data was successfully saved.")
	else
		print("The data was not saved due to an error.")
		warn(errormessage)
	end
end)

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()
			if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 8552651) then
				print("This user did not lose any coins, for they own the game pass!")
			else
				local coins = {1, 2, 3}
				local random = math.random(1, #coins)
				local picked = coins[random]
				
				plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - picked
			end
		end)
	end)
end)

for i,v in pairs(game.Workspace:GetChildren()) do
	if v.Name == "LavaPart" then
		v.CanCollide = false
		local clone = script.Script:Clone()
		clone.Parent = v
	end
end

Level Handler

local Checkpoints = script.Parent

for i,v in pairs(Checkpoints:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if player then
					if player.leaderstats.Level.Value < tonumber(v.Name) then
						player.leaderstats.Level.Value = tonumber(v.Name)
						if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 8552645) then
							player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 4
						else
							player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 2
						end
					end
				end
			end
		end)
	end
end

The code here does not look relevant at all to what your issue is.

I suggest inserting a SpawnLocation instance and dragging it where you want player’s to spawn.

I’m not sure that you read all of my code. I am using parts, not spawns. This code is fully relevant, as it’s the game’s operators.

1 Like

Ah, greatly sorry! I skimmed to fast. I see now!

Okay. I tested the code myself and I think I found the issue? You need to wait until the Character is a child or descendant of Workspace.

repeat wait() until character:IsDescendantOf(workspace)
--// after this line change the HumanoidRootPart's CFrame

Without doing so I also ran into the issue of the character spawning into the air rather than the designated point.

Also, for future reference with datastores, you can store arrays and dictionaries into a datastore instead of storing 50 things into their own separate datastores.
And you can also use SetPrimaryPartCFrame on the character with the above (or just MoveTo)

2 Likes