King of hill script constant loop causing lag

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to achieve a king of the hill system

  2. What is the issue?
    it works its just this script isn’t efficient as its constantly looping and would cause my game to lag

  3. What solutions have you tried so far?
    havent found one yet

while task.wait() do
	task.wait(0.1)
	local mag = (workspace.KingOfTheHill.KingPart.Position-script.Parent.Parent.Parent.Character.HumanoidRootPart.Position).Magnitude
	if mag <= 20 then
		if workspace.KingOfTheHill.KingPart.Owner.Value == "nil" then
			workspace.KingOfTheHill.KingPart.Owner.Value = script.Parent.Parent.Parent.Name
			workspace.KingOfTheHill.KingPartglow.BillboardGui.TextLabel.Text = "Current King : "..workspace.KingOfTheHill.KingPart.Owner.Value..""
		end
	else
		if workspace.KingOfTheHill.KingPart.Owner.Value == script.Parent.Parent.Parent.Name then
			workspace.KingOfTheHill.KingPart.Owner.Value = "nil"
			workspace.KingOfTheHill.KingPartglow.BillboardGui.TextLabel.Text = "Current King : None"

		end
	end
end

1 Like

obviously it will lag
You are never caching anything and are indexing same exact values each time
also
image
what is this nonsense?

3 Likes

this script is pretty bad, looks like a free model

Try using game.Workspace:GetPartBoundsInBoundingBox (if it’s box-shaped) or game.Workspace:GetPartBoundsInRadius. It’s Roblox’s most modern way to detect objects in a zone. Then you should also create a table in which you put the players’ HumanoidRootPart.

Basically, at the start of a game, you should do a for loop like that:

for _, Player in game.Players:GetChildren() do
-- stuff
end

You should, in a table, put each player and its HumanoidRootPart like {[Player] = Player.Character.HumanoidRootPart} and create a connection that fires each time the player respawns via CharacterAdded. When it triggers, replace the old HumanoidRootPart by using MyTable[Player] = Player.Character.HumanoidRootPart.

When the player leaves, its value in the table will be freed automatically. Now, at a frequency of 0.25 seconds for example, you’ll use one of the functions I suggested before, with:

  • As a first argument, where it is. Use CFrame if it’s a box, and Vector3 if it’s a radius.
  • As a second argument, how big it is. Use Vector3 if it’s a box, and a number (20 in your case) if it’s a radius.
  • And OverlapParams you can create and configure so:
local Overlap = OverlapParams.new()

Overlap.FilterDescendantsInstances = [YOUR HUMANOIDROOTPARTS TABLE]
Overlap.FitlerType = Enum.RaycastFilterType.Include

Now it should detect if a player is inside a zone without causing lag. You will indeed use one runtime for everyone, which will greatly affect performances.

Hope it helps! (also just saying that the script is bad doesn’t help at all guys)

3 Likes

Thank you but i still dont know how to add my script into this

Seeing your lasts few posts, you put almost everything in a loop. Know that doing that will obviously cause lag.

You should use something like ZonePlus

If you really want to learn, i recommend you stop copying pasting scripts cause you will never learn that way.

i know i have a big issue w my game with so many loops ive just got few more left to sort out

Well, as i said, use zoneplus, it is a module script that already got the stuff, all you need to do is read the documment and implement your logic to it.

still havent found a solution for this

Oh, wait. You need to do what I suggested in a server script in server script service to make it work.

Do you perhaps need help coding this?

1 Like

Yeah ngl i do need help idk how to structure the code. I also tried to make a remote even if local player touches like the king of hill part itll fire server but it didnt work. But i still need to try your but im stuck on how to structure it

I wouldn’t recommend this since it creates a lot of lag.

Anyway, maybe try this:

local HumanoidRootParts = {}

local Zone = game.Workspace.KingOfTheHill.KingPart
local OwnerValue = Zone.Owner

local function ConnectRootPartManager(Player)
	Player.CharacterAdded:Connect(function(Character)
		table.insert(HumanoidRootParts, Character.HumanoidRootPart)
	end)
end

-- ONLY ADD IF YOU AREN'T TRIGGERING THIS AT THE START OF A SERVER
coroutine.wrap(function()
	for _, Player in game.Players:GetChildren() do
		ConnectRootPartManager(Player)
	end
end)()

game.Players.PlayerAdded:Connect(ConnectRootPartManager)

while task.wait(0.25) do
	local ZoneParams = OverlapParams.new()

	ZoneParams.FilterDescendantsInstances = HumanoidRootParts
	ZoneParams.FilterType = Enum.RaycastFilterType.Include

	local ZoneResult = game.Workspace:GetPartBoundsInRadius(Zone.Position, 20, ZoneParams)

	if #ZoneResult == 1 then
		OwnerValue.Value = game.Players:GetPlayerFromCharacter(ZoneResult[1].Parent).Name
	else
		OwnerValue.Value = "nil"
	end
end

I tried it in my end and it works perfectly. No sight of lag. You now just need to change the script to make it fit with other effects and such.

EDIT: You can change the radius (20) by the way.

This doesnt seem to be working for me for some reason there is no errors aswell

Where did you put the script? And is the value blank or “nil”? That would be very useful to solve the problem.

the value is nil still and i put the script here

This. That script won’t work where it is. My script was designed to be put in ServerScriptService, and actually not in StarterGui. Try with a script in ServerScriptService and check again. :+1:

EDIT: If it shows “nil”, make sure you removed the coroutine I marked with a comment.

Its still not working i remove the coroutine aswell and i put it in server script server

the owner has changed to SubTo_Lurrz but the text in game doesnt change

As I said before, you’re up to change everything you need to do as it works as you expect. I scripted the base, and it’s up to you to script the rest (mostly because there are some things like the crown that is from another script so you might want to check when the Owner value changes to know if it has to be shown).

Basically, my script works as expected, but you need to add a few lines to make it work as you expect.

also its making the game even more laggy for some reason when i step on the part