Copious Lagspikes

In my game (ai squid game - Roblox) I keep experiencing enlarged and abnormal amounts of ping (8000ms) due to the fact that there is a server script in every single humanoid. Network receive is also around the 90kbps - 150kbps mark. Everything is server sided so I am unable to optimize networking.

I can provide code snippets.

Then do provide code, there is very little to work with here other than an explanation…

Do you have a script in which it calls a RemoteEvent?

You need to stay under 50kbs. You need to optimise your networking via reducing remotes or using tools like bytenet max

Server

-- It looks terrible, I know
local rep = game:GetService("ReplicatedStorage")
local start = rep:WaitForChild("started")
local grn = rep:WaitForChild("green")
local fg = false
local col = game:GetService("CollectionService")
local s = workspace:WaitForChild("sound")
_G.deaths = 0
fg = false

function Format(Int)
	return string.format("%02i", Int)
end

function convertToHMS(Seconds)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds = Seconds - Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes = Minutes - Hours*60
	return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
end

start.Changed:Connect(function()
	grn.Value = false
	fg = false
	if start.Value == true then
		task.spawn(function()
			task.spawn(function()
				for i = 1, 60 do
					task.wait(1)
					local ac_s = 60 - i
					local t = workspace:WaitForChild("Clock").time.SurfaceGui.TextLabel
					t.Text = convertToHMS(ac_s)
				end
			end)
			task.wait(60)
			start.Value = false
			for _, bot: Humanoid in pairs(col:GetTagged("AIHumanoid")) do
				if not bot:HasTag("survived") then
					bot:TakeDamage(1000)
				end
			end
		end)
		while true do
			task.wait()
			if start.Value == false then
				break
			end
			s:Play()
			task.wait(s.TimeLength)
			print("play")
			grn.Value = false
			fg = true
			for _, bot: Humanoid in pairs(col:GetTagged("AIHumanoid")) do
				task.wait(0.02)
				if not bot:HasTag("survived") then
					if bot.MoveDirection.Magnitude > 0 then
						local rng = math.random(0, 3)
						if rng == 2 and game.Teams.Guard:GetPlayers() ~= {} then
							local highlight = Instance.new("Highlight")
							highlight.Parent = bot.Parent
							highlight.Enabled = true
							highlight.Adornee = bot.Parent
							print("marking "..bot.Parent.Name.." for execution")
							task.spawn(function()
								task.wait(12)
								highlight:Destroy()
								workspace.shoot:Play()
								bot:TakeDamage(1000)
							end)
						else
							task.spawn(function()
								workspace.shoot:Play()
								bot:TakeDamage(1000)
							end)
						end
					end
				end
			end
			task.wait(1)
			fg = false
			grn.Value = true
		end
	end
end)

Client

-- A bit of ChatGPT considering i suck at coding
local npc = script.Parent
local humanoid = npc:FindFirstChildOfClass("Humanoid")
local rep = game:GetService("ReplicatedStorage")
local start = rep:WaitForChild("started")
local green = rep:WaitForChild("green")
local stop = true
local fg: boolean = _G.GreenLight
local s = workspace:WaitForChild("sound")
local BloodEngine = require(rep.BloodEngine)
local col = game:GetService("CollectionService")
local speedmult = 10

humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
start.Changed:Connect(function()
	-- Wait for start.Value to be true
	s.Played:Connect(function()
		if start.Value == true and not humanoid:HasTag("survived") then
			local bodyPosition = humanoid.Parent.PrimaryPart.Position
			local moveDirection = humanoid.Parent.PrimaryPart.CFrame.LookVector -- The forward direction
			speedmult = speedmult + (_G.deaths * 0.5)
			print("setting "..humanoid.Parent.Name.." speed multiplier to: "..speedmult)
			-- Set a timer to stop movement after the specified time
			local startTime = tick()

			-- Move the humanoid for the given time
			while tick() - startTime < s.TimeLength do
				task.wait()
				-- Set the humanoid's velocity in the forward direction
				humanoid:Move(Vector3.new(moveDirection.X, 0, moveDirection.Z) * 10)  -- Adjust speed (10) as needed
				wait(0.1)
			end

			humanoid:Move(Vector3.new(0, 0, 0))
			local gambling = math.random(0, 4)
			if gambling == 3 then
				local startTime2 = tick()

				-- Move the humanoid for the given time
				while tick() - startTime2 < 1 do
					task.wait()
					-- Set the humanoid's velocity in the forward direction
					humanoid:Move(Vector3.new(moveDirection.X, 0, moveDirection.Z) * 10)  -- Adjust speed (10) as needed
					wait(0.1)
				end
				humanoid:Move(Vector3.new(0, 0, 0))
			end
		end
	end)
end)

humanoid.Died:Connect(function()
	humanoid:RemoveTag("AIHumanoid")
end)

There are quite literally ZERO remotes in the experience

I cant even fathom what this code is trying to achieve. Regardless, it is terribly written.

2 Likes

I do agree, I was in a rush and made this as a joke but it ended up becoming a full blown project.