My capture the flag system is a bit slow, what could be changed?

My script generates flags all over the map and makes floor wires to them, but it makes the server lag when it’s run, is there a way to optimise the code? I’ll send the script as a file. flagsystem.rbxl (24.6 KB)

There was so much wrong with your script. Your code even looked free modeled.

  • Global Workspace is deprecated.
  • HopperBins are deprecated.
  • Why so many do’s? That could possibly be why lag comes.
  • Never use :Remove() for it is deprecated; use :Destroy() or :ClearAllChildren() depending on what you are doing.
  • Don’t use CustomEvent, use BindableEvent, for CustomEvent is deprecated also.
  • You were destroying v even though it is already nil. That was also why your code at the bottom doesn’t run.
  • FlagStands are also deprecated, so you have to create your own system yourself using FlagStandServive. Roblox even provides a template that you can use for it. I don’t have time to create a system so I left that un-modified.
  • Don’t use FloorWires for those are also deprecated.

Questionable code

  • Why did you specify CFrame even though you can just do v.CFrame?
  • p:FindFirstAncestor("")… This is un-used.
  • Even though I fixed the bottom and made it function, it also seems unused. You also don’t need to get the children of game also.

The new code

local fss = game:GetService("FlagStandService")

for i,v in pairs(workspace.FlagPoints:GetChildren()) do
	spawn(function()
		delay(0,function()
			wait(1)
			v.Transparency = 1
			local pos = v.Position
			local f = Instance.new("FlagStand")
			local h = Instance.new("Tool")
			h.Name = "Flag"
			local p = game:GetService("PointsService")
			f.Parent = workspace
			f.Anchored = true
			f.CFrame = v.CFrame
			for e = 1,10000 do
				v:Destroy()
			end
		end)
	end)
end

local bindableEvent = Instance.new("BindableEvent",workspace)

local gameChildren = game:GetChildren() --???


Suggestions:

Put this code in ServerScriptService and re-name the script to keep your things organized.


Roblox even provides tutorials that you can use for additional help:

The delay is also pointless because its buffer time is 0, making it happen immediately. Also, don’t use spawn, use coroutines.

1 Like

i wrote it myself from an old crazyman32 tutorial

“The new code”
quick question. is this a joke or are you serious. :skull: