Server spawn loop code improvements

this is server code put inside a PlayerAdded event. How could i improve the reliability of the anti-teleport and ragdoll event?

roblox doesn’t fire .Died on the client if the character is reparented quick enough on the server (reason for the event at the end)

player.characterAdded:Connect(function(character)

		local lastPos

		-- waiting to set spawn location
		runService.Heartbeat:Wait()
		footplant(player, character)
		character:WaitForChild("Animate"):Destroy()

		character.Humanoid.BreakJointsOnDeath = false
		character.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
		character.Archivable = true

		local billboard = replicatedStorage:WaitForChild("cache"):WaitForChild("playerBillboard"..player.currentTeam.Value):Clone()
		billboard.Parent = character.Torso
		billboard.nameLabel.Text = character.Name

		connections[#connections+1] = character.Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()

			if not functionCollection.isAlive(player, true) then return end

            if character:FindFirstChild("Humanoid") and character.Humanoid.FloorMaterial == Enum.Material.Air then
				local lastPosition = character.HumanoidRootPart.Position
				local distance = 0
				while character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") and character.Humanoid.FloorMaterial == Enum.Material.Air do
					local delta = lastPosition.y - character.HumanoidRootPart.Position.y
					distance = distance + math.abs(delta)
					lastPosition = character.HumanoidRootPart.Position
					wait()
				end

				print(player.Name.." ; "..distance)

				local damage = distance * 5
				if distance > 5 and not fallCooldown then
					character.Humanoid:takeDamage(damage)
				end

			end

		end)

		lastPos = character.HumanoidRootPart.Position
		connections[#connections+1] = runService.Heartbeat:Connect(function()

			if nextPos and functionCollection.isAlive(player, true) then

				player.HumanoidRootPart.CFrame = nextPos
				lastPos = player.HumanoidRootPart.Position
				nextPos = nil
			end

			if functionCollection.isAlive(player, true) and nextCheck < tick() then

				nextCheck = tick() + 5

				local verticalDistance = (lastPos.y - character.HumanoidRootPart.Position.y)

				local horizontalDistance = ((lastPos - Vector3.new(0, lastPos.y, 0)) - (character.HumanoidRootPart.Position - Vector3.new(0, character.HumanoidRootPart.Position.y, 0))).Magnitude
				local maxDistance = (maxWalkspeed * 5) + 1

				lastPos = character.HumanoidRootPart.Position

				if not character:FindFirstChild("Humanoid") then return end
				if not player then return end

				if character.Humanoid:getState() ~= Enum.HumanoidStateType.Seated and character.Humanoid.Health > 0 then

					if verticalDistance < -maxVertDistance
						or horizontalDistance > maxDistance
					then
						penalty()
					end

				end

			end
		end)

		local humanoid = character.Humanoid
		humanoid.ancestryChanged:Connect(function()
			if character:IsDescendantOf(workspace) and humanoid.Health > 0 then
				serverScriptService.functions.sendServerWarning:fire(player.Name.." was kicked by anti-godmode")
				player:kick("godmode")
			end
		end)

		character.Humanoid.Died:wait()
		if not character:isDescendantOf(workspace) then return end

		player.deaths.Value = player.deaths.Value + 1

		runService.Heartbeat:wait()

		character.Parent = nil
		player.Character = nil

		remotes.generic.playerDeath:fireAllClients(player, character)

		--[[
		ragdoll:activate(character)
		if character.flybackObject.Value then
			character.flybackObject.Value.Velocity = character.flybackVector.Value * 100
		end

		for _, v in pairs(connections) do v:disconnect() end
		--]]

	end)
1 Like