Hi everyone,
I’m experiencing an issue with a script I’m using to dynamically load and unload containers based on the player’s position in my Roblox game. The script works different in studio and in game Specifically, some of the defined regions are not correctly triggering container loads when the player enters those areas.
Video link: https://youtu.be/JxBOMBZmRd8
The script:
local LocalPlayer = game.Players.LocalPlayer
local debugMode = true -- Debug modunu açmak veya kapatmak için
local RunService = game:GetService("RunService")
function getRegion(RegionBox)
local Overhang = 0.2
local StartCFrame = RegionBox.CFrame:ToWorldSpace(CFrame.new(-(RegionBox.Size.X /2) - Overhang, -(RegionBox.Size.Y /2) - Overhang, -(RegionBox.Size.Z /2) - Overhang))
local EndCFrame = RegionBox.CFrame:ToWorldSpace(CFrame.new((RegionBox.Size.X /2) + Overhang, (RegionBox.Size.Y /2) + Overhang, (RegionBox.Size.Z /2) + Overhang))
local StartVector = Vector3.new(StartCFrame.X, StartCFrame.Y, StartCFrame.Z)
local EndVector = Vector3.new(EndCFrame.X, EndCFrame.Y, EndCFrame.Z)
return Region3.new(StartVector, EndVector)
end
local RegionList = {}
local ContainerMap = {}
function GetRegionList()
for i, Part in pairs(script.Bounds.Value:GetChildren()) do
RegionList[#RegionList + 1] = {
Region = getRegion(Part),
Name = Part.Name
}
if debugMode then
print("Added Region: " .. Part.Name)
end
end
end
function UpdateContainerMap()
ContainerMap = {}
for _, Container in pairs(script.Container.Value:GetChildren()) do
if Container:IsA("Model") or Container:IsA("Folder") then
ContainerMap[Container.Name] = Container
end
end
if debugMode then
print("ContainerMap:")
for name, container in pairs(ContainerMap) do
print("Name: " .. name .. " Type: " .. container.ClassName)
end
end
end
function WaitForContainer(name, timeout)
local startTime = tick()
while tick() - startTime < timeout do
local container = script.Container:FindFirstChild(name)
if container then
return container
end
wait(0.1)
end
warn("Timed out waiting for container: " .. name)
end
function UpdateVisual(InBounds, ContainerName)
-- Unload all containers first
for _, Container in pairs(script.Container.Value:GetChildren()) do
Container.Parent = nil
end
if InBounds then
local containerToLoad = ContainerMap[ContainerName]
if containerToLoad then
containerToLoad.Parent = script.Container.Value
if debugMode then
print("Loaded Container: " .. ContainerName)
end
else
warn("Container not found: " .. ContainerName)
if debugMode then
print("All container names:")
for _, Container in pairs(script.Container.Value:GetChildren()) do
print(Container.Name)
end
end
end
else
if debugMode then
print("Not in bounds, no container loaded.")
end
end
end
GetRegionList()
UpdateContainerMap()
UpdateVisual(false, "")
local OldBounds = false
local OldContainerName = ""
RunService.Heartbeat:Connect(function()
if LocalPlayer.Character and LocalPlayer.Character:FindFirstChild("Head") then
local MainPart = LocalPlayer.Character:FindFirstChild("Head")
local InBounds = false
local ContainerName = ""
for _, RegionData in pairs(RegionList) do
local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(RegionData.Region, {MainPart}, 1)
if #partsInRegion > 0 then
InBounds = true
ContainerName = RegionData.Name
break
end
end
if debugMode then
if InBounds then
print("In Bounds with Container: " .. ContainerName)
else
print("Not in any bounds")
end
end
if InBounds ~= OldBounds or ContainerName ~= OldContainerName then
OldBounds = InBounds
OldContainerName = ContainerName
UpdateVisual(InBounds, ContainerName)
end
end
end)