Spawn Character Problem

Good Morning Dev , So i made a Find the key Game with obby mechanics and CheckPoints But theres a problem when the character spawn on roblox At his CheckPoint The Camera is completely laggy

here is the code

local Players = game:GetService("Players")
local AdminsTable = {4443001324}
local Rep =  game:GetService("ReplicatedStorage")
local NewStats = require(Rep.NewStats)
local CheckPoints = workspace:FindFirstChild("CheckPoints")
local DataStore = game:GetService("DataStoreService"):GetDataStore("SavingStager")


local function Stats(player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Parent = player
	Leaderstats.Name = "leaderstats"

		NewStats:New(player,"Stage",1)
	
end

local function OnPlayerAdded(player)
	 Stats(player)
	local succes, errorMsg = pcall(function()	
		local StageData = DataStore:GetAsync(player.UserId)
	player.leaderstats.Stage.Value = StageData
		player:SetAttribute("CurrentStage",player.leaderstats.Stage.Value)



	end)
end

	local function SpawnCharacter(Character:Model,player:Player)
	



		local Hrp:Part = Character:WaitForChild("HumanoidRootPart")
		Character:MoveTo(CheckPoints[tostring(player:GetAttribute("CurrentStage"))].Position + Vector3.new(0,6,0))
	print("Spawning "..player.Name)

	

end


Players.PlayerAdded:Connect(function(player)
	
	
	
	player:GetAttributeChangedSignal("CurrentStage"):Connect(function()



		local Char = player.Character or player.CharacterAdded:Wait()
	local Hrp = Char:FindFirstChild("HumanoidRootPart")
		Hrp.Position = CheckPoints[tostring(player:GetAttribute("CurrentStage"))].Position
		
		if Char:FindFirstChild("KeyTool") then
			Char:FindFirstChild("KeyTool"):Destroy()
		end
	end)

OnPlayerAdded(player)


player.CharacterAdded:Connect(function(Char)

	SpawnCharacter(Char,player)
end)

end)


Players.PlayerRemoving:Connect(function(Player)
	local StageValue = Player:FindFirstChild("leaderstats").Stage.Value
	local succes, errorMsg = pcall(function()	

DataStore:SetAsync(Player.UserId,StageValue)


	end)
	if succes then
		print("Loaded "..Player.Name.." Data on quit")
		
	else
		warn(errorMsg)
end		
end)

Players.PlayerAdded:Connect(function(plr)
	
local Stage:IntValue = plr:WaitForChild("leaderstats").Stage



Stage.Changed:Connect(function(val)
	
	plr:SetAttribute("CurrentStage",val)
end)
end)

game["Run Service"].Heartbeat:Connect(function()
for index , player in pairs(game.Players:GetPlayers()) do

		if player.leaderstats.Stage.Value == 0 then
			player.leaderstats.Stage.Value = 1
			print("changed")
		end
	end
	end)



i tried to wait until the character is completely loaded but its still laggy but whenever i die and i respawn the camera is fixed. and i am really confused about this problem thanks for any response..

I think the problem is located at the function SpawnCharacter() or at Player.CharacterAdded

This codes seems to be the problem. Instead of moving the HumanoidRootPart, try moving the character using the Character:MoveTo() function.

I fixed the problem it was : `player:GetAttributeChangedSignal(“CurrentStage”):Connect(function()

	local Char = player.Character or player.CharacterAdded:Wait()
local Hrp = Char:FindFirstChild("HumanoidRootPart")
	Hrp.Position = CheckPoints[tostring(player:GetAttribute("CurrentStage"))].Position
	
	if Char:FindFirstChild("KeyTool") then
		Char:FindFirstChild("KeyTool"):Destroy()
	end
end)`

but than you btw

Would still be a good idea to use Character:MoveTo(), Moving the rootpart directly may cause other bugs

1 Like