I’ve decided to delete this script, because I don’t want to share my code. Please understand that.
Pretty interesting, I’ve read through the code a bit, here’s some feedback!
First and foremost, you defined
local Parent = script.Parent.Parent.Parent
But you never really used it
I would definitely look to define variables and use them in places like this.
LoadScript:UnloadStube(script.Parent.Parent.Parent["4"]) script.Parent.Door1.Transparency = 0 script.Parent.Door1.CanCollide = true script.Parent.Handle1.Transparency = 0 script.Parent.Handle1.CanCollide = true script.Parent.Door2.Transparency = 1 script.Parent.Door2.CanCollide = false script.Parent.Handle2.Transparency = 1 script.Parent.Handle2.CanCollide = false script.Parent.Open.Value = false script.Parent.Parent.Parent.Loaded.Value = false
Well one of the things that I think are contributing to the lag is this one line
local parts = workspace:FindPartsInRegion3(Region, nil, math.huge)
This means that it will cache ALL the parts that are in the region3
Then you are looping through ALL of those parts.
for _, part in pairs(parts) do --this is really inefficient since you need to search all the parts if part.Parent:FindFirstChild("HumanoidRootPart") and game.Players:FindFirstChild(part.Parent.Name) then local plr = game.Players:FindFirstChild(part.Parent.Name) table.insert(playersInRegion,plr) print(plr.Name) end end
However, I think that a better option would just to be to go through all the players, and determine if the humanoidRootPart is in the region. (Smart zone function here which I will use How do I get a player from a zone? - #7 by blokav)
function isInsideBrick(position, brick) -- this is from the other post local v3 = brick.CFrame:PointToObjectSpace(position) return (math.abs(v3.X) <= brick.Size.X / 2) and (math.abs(v3.Y) <= brick.Size.Y / 2) and (math.abs(v3.Z) <= brick.Size.Z / 2) end for _, player in pairs(Players:GetPlayers()) do local character = player.Character; local humanoidRootPart = character ~= nil and character:FindFirstChild("HumanoidRootPart"); if humanoidRootPart and isInsideBrick(humanoidRootPart, RegionPunkt) then print(player.Name.." is in region"); else -- continue code ... end end
This should definitely improve some performance. Don’t use region3 unless you have to really tbh.
That’s all for now, good luck with your project!
This won’t really help if you’re trying to load the room locally, when a server does something, it gets replicated across clients, therefore if a client joins a room, and the server loads it, it will be loaded on all clients.
Would highly recommend thinking that especially if you have a game with 6 rooms +, and a lot of players.