Can someone help me optimize my code

Hi, so i am making a hide and seek game but i need help optimizing my code.

wait(0.2)
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local Map = script.Map
local LobbySpawn = game.Workspace.SpawnLocation
local SeekerRealeased = script.SeekerRealeased
local AlivePlayers = {}
local Seekerplayer = nil
local CanStart = script.CanStart
local CanTag = false
local Table_1 = {roundLength = 300, seekerlength = 10, intermissionLength = 10}
local Table_2 = {}


task.spawn(function()
	while wait(4) do---
		if #game.Players:GetPlayers() >= 3 then
			CanStart.Value = true
		else
			CanStart.Value = false

		end
	end
end)


local GameSpawn =  game:GetService("Workspace"):WaitForChild("Maps"):WaitForChild(Map.Value).PlayerSpawn

local function roundTimer()
		for i = Table_1["intermissionLength"], 1, -1 do
			InRound.Value = false
			task.wait(1)
			Status.Value = "Intermission: "..i.. " seconds left!"
		end
	for i = Table_1["seekerlength"], 1, -1 do
			InRound.Value = true
			task.wait(1)
			Status.Value = "The seeker will be realeased in: "..i.. " seconds left!"
		end
	for i = Table_1["roundLength"], 1, -1 do
			InRound.Value = true
			SeekerRealeased.Value = true
			task.wait(1)
			Status.Value = "The games has: "..i.. " seconds left!"
		end
	end

if Table_1["roundLength"] < 2 then
	InRound.Value = false
end
print(CanStart.Value)
print(#game.Players:GetPlayers())
CanStart.Changed:Connect(function()
	if CanStart.Value  == true then

		spawn(roundTimer)
		InRound.Changed:Connect(function()
			wait(1)
			if InRound.Value == true then
				for _, player in pairs(game.Players:GetPlayers()) do
					local char = player.Character
					char:WaitForChild("HumanoidRootPart").CFrame = GameSpawn.CFrame + Vector3.new(0 , 3 , 0)
					local randomNum = math.random(1,#game.Players:GetPlayers())
					for i,v in pairs(game.Players:GetPlayers()) do
						print(i)
						v.CameraMaxZoomDistance = 8
						table.insert(AlivePlayers,i,v.Name)
						if i == randomNum then
							v:AddTag("Seeker")
							Seekerplayer = v
							table.remove(AlivePlayers,i)
							Seekerplayer.Character.HumanoidRootPart.CFrame = game:GetService("Workspace"):WaitForChild("SeekerBox"):WaitForChild("Tppart").CFrame + Vector3.new(0,5,0)
							SeekerRealeased.Changed:Connect(function()
								if SeekerRealeased.Value == true then
									if player:HasTag("Hider") then
										char:WaitForChild("HumanoidRootPart").CFrame = GameSpawn.CFrame + Vector3.new(0 , 3 , 0)
									end
									CanTag = true
									Seekerplayer.Character:WaitForChild("HumanoidRootPart").CFrame = game:GetService("Workspace"):WaitForChild("Maps"):WaitForChild(Map.Value):WaitForChild("SeekerSpawn").CFrame+ Vector3.new(0,5,0)
									
									Seekerplayer.CanTag.Value = true
								end
							end)
						else
							v:AddTag("Hider")
						end
						
					end
				end
			else
				for _, player in pairs(game.Players:GetPlayers()) do
					
					local char = player.Character
					char:WaitForChild("HumanoidRootPart").CFrame = LobbySpawn.CFrame + Vector3.new(0 , 3 , 0)
					SeekerRealeased.Value = false
					CanStart = false
					CanTag = false
					Seekerplayer.CanTag.Value = false
				
					
				end
			end
		end)
	else 
		Status.Value = "Not enough players ("..#game.Players:GetPlayers()..")"
	end
end)
task.spawn(function()
	while wait(2) do
		for _, obj in pairs(workspace:GetChildren()) do
			if obj:FindFirstChild("Humanoid") and obj:FindFirstChild("HumanoidRootPart") then
				
				local HiderTorso = obj:FindFirstChild("UpperTorso")
				HiderTorso.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("HumanoidRootPart") then
						print(hit.Parent)
						local SeekerChar = hit.Parent
						local HiderChar = obj
						local SeekerPlr = game.Players:GetPlayerFromCharacter(SeekerChar)
						local HiderPlr = game.Players:GetPlayerFromCharacter(HiderChar)
						if HiderPlr then
							print(HiderPlr)
							print(SeekerPlr)
							if HiderPlr:HasTag("Hider") and SeekerPlr:HasTag("Seeker") and CanTag == true and SeekerPlr.CanTag.Value == true then
								game:GetService("ReplicatedStorage").Remotes:WaitForChild("FireGUI"):FireAllClients(HiderPlr.DisplayName)
								HiderTorso.CFrame = LobbySpawn.CFrame + Vector3.new(0,5,0)
								for i,v in pairs(AlivePlayers) do
									if v.Name == HiderPlr.Name then
										table.remove(AlivePlayers,i)
									end
								end
							end
						end
					end
				end)
			end
		end
	end
end)

Thanks if wanna help!

1 Like

Can you describe why you think this script is ineffecient enough to need optimization?

The only major flaw I can see is putting a while loop around the HiderTorso.Touched connection, you shouldn’t need the task.spawn or while wait(2) do before it since it will repeat the connection

It uses many while loops. and threads

Threads and while loops aren’t bad on their own, I think you only have one very bad thread+while loop I mentioned in my edit; which is actually more of a :Connect optimization issue.

Some minor loop odities, just need to be better asking yourself “Does this need to be in or outside the loop” for example this loop when the round ends is mostly repeated false sets; but this is hardly a performance concern.

for _, player in pairs(game.Players:GetPlayers()) do
	local char = player.Character
	char:WaitForChild("HumanoidRootPart").CFrame = LobbySpawn.CFrame + Vector3.new(0 , 3 , 0)

	-- all below variables could be set outside the for loop
	SeekerRealeased.Value = false
	CanStart = false
	CanTag = false
	Seekerplayer.CanTag.Value = false
end
1 Like

Thank you! Thanks for ur tips!

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