Cloning model(s) freezes server for a few seconds

Hello, I am recoding a game (basically from the ground up, script wise) and for some reason the server completely freezes when players spawn

It stops attacks, sprinting, all and anything to do with the server while they spawn,
It doesnt do this in the main game but does here for some reason, it uses the exact same models and I’m not sure why this is happening or if anyone here can help

I doubt it’s the code but here’s it incase, any tips would be extremely helpful, thank you
Quick side note: The bigger the model, the longer the lag/freeze lasts. Even occurs in studio

if not dinosaur then
		dinosaur = "Gallimimus"
	end
	
	local dinosaurFolder = dinosaurs[dinosaur]
	
	local data = player.Data
	local session = data.Session
	local spawned = session.Spawned
	local ownsDino = data.Dinosaurs[dinosaur].Owned

	if spawned.Value then
		player:Kick('Attempted to spawn but you were already spawned.')
		return
	end
	spawned.Value = true
	player.Data.Session.Safe.Value = true

	if not ownsDino.Value then
		player:Kick(("Attempted to spawn as %q but you do not own this dinosaur."):format(tostring(dinosaur)))
		return
	end

	local morph = dinosaurFolder.Dinosaur:Clone()
	local hp = playerTemplate:Clone()
	local animations = dinosaurFolder.Animations:Clone()
	local control = serverStorage.Misc["Private;Control"]:Clone()
	local humanoid:Humanoid = morph.Humanoid
	local healthBar = {
		clipping = hp.Stat.Health.CurrentHealth;
	}
	
	morph.PrimaryPart.Anchored = true
	healthBar.clipping.BackgroundColor3 = Color3.fromRGB(11, 194, 188)
	hp.PlayerToHideFrom = player
	hp.Parent = morph
	hp.Adornee = morph:FindFirstChild("FakeHead") or morph:FindFirstChild("Hed")
	hp.Stat.TexName.Text = player.Name .. "\nLOADING"
	
	--task.spawn(function()
	--	repeat
	--		runService.Heartbeat:Wait()
	--		local state = humanoid:GetState()
	--		if state == Enum.HumanoidStateType.Jumping then
	--			if session.Jumping.Value then continue end
	--			-- create warning/use warning system
	--		end
	--	until not player.Character or not player.Character.Parent
	--end)

	local info = database[dinosaur]
	local stamina = player.Data.Session.Stamina

	stamina.Max.Value = info.Stamina
	stamina.Value = info.Stamina
	humanoid.MaxHealth = info.Health
	humanoid.Health = info.Health
	humanoid.WalkSpeed = 0
	animations.Parent = morph.Humanoid
	player.Character = morph
	events.DisableConnections:FireClient(player, false)

	pcall(function()
		morph.Parent = workspace
	end)

	local ray	
	repeat
		ray = testSurface()
		task.wait()
	until ray and ray.Position
	
	local pos = ray.Position
	local y = info.HipHeight
	pos += Vector3.new(0, y, 0)
	teleportPlayer(player, pos)
	player.Character:SetPrimaryPartCFrame(player.Character:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(math.random(1, 360)), 0))
	
	session.Spawned.Value = true
	session.Spawned.StatsReset.Value = false
	session.Spawned.Dinosaur.Value = dinosaur
	control.Parent = morph.Humanoid
	
	task.spawn(function()
		while task.wait(1) do
			if not (player.Character or player.Character.Parent) then
				break
			end
			if player.Data.Session.SpawnLoaded.Value == true then
				break
			end
		end
		if not player.Character then return end
		if not player.Character.Parent then return end
		hp.Stat.TexName.Text = player.Name .. "\nIMMUNE"
		task.wait(5)
		if not player.Character then return end
		if not player.Character.Parent then return end
		
		hp.Stat.TexName.Text = player.Name
		player.Data.Session.Safe.Value = false
		healthBar.clipping.Size = UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0)
		healthBar.clipping.BackgroundColor3 = Color3.fromRGB(123, 180, 117)
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			healthBar.clipping.Size = UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0)
			healthBar.clipping.BackgroundColor3 = Color3.fromRGB(180, 47, 47):Lerp(Color3.fromRGB(97, 180, 88), humanoid.Health / humanoid.MaxHealth)
		end)
	end)
	task.spawn(function()
		while task.wait(5) and humanoid.Parent do
			if session.InCombat.Value then continue end
			humanoid.Health += math.floor(humanoid.MaxHealth / 20)
		end
	end)
1 Like

Try setting Network Ownership to the Server or the Player, it should help fix a Bit of the “freezing”

Didn’t make a difference,

I did add a ton of prints and waits to see “what” is causing it and it appears to be doing it when I parent it to workspace, not sure if this helps anyone understand this situation

I’ve found more information, going to close this and redo the whole topic with the newly found information (different problem now)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.