You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
I want to achieve a king of the hill system
What is the issue?
it works its just this script isn’t efficient as its constantly looping and would cause my game to lag
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
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:
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)
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.
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.
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.
EDIT: If it shows “nil”, make sure you removed the coroutine I marked with a comment.
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.