Draw Distance Rendering issues

What I want to achieve is a draw distance rendering system(? dont know if thats the name) but my current problem is
A. my for i,v in pairs(workspace:GetDescendants()) is somehow only changing a single part(BetterBaseplate)
B. its not unrendering models that are far away from the character (Update: got this one fixed but now its not coming back.)
C. material isn’t changing
D. stuff isn’t reverting when I change configuration/get near to stuff.

Default
image
Test mode becomes 0 Reflectance

Note I don’t normally post in scripting support because I get ashamed of how messy and unoptimized my code is(considering i didn’t start optimizing and cleaning yet), but this time I simply can’t understand what I am doing wrong here.

Material is true when it shouldn’t render materials
Detail is true when it shouldn’t render details.
Update: I realized I didn’t use spawn(function() before the while wait loop, so that was causing some of the problems, but I ended up with more now.

 function render(qualityLevel, char)
if distance == nil and qualityLevel then
	distance = (qualityLevel*10)+40
end
if detail then
	game.Lighting.EnvironmentDiffuseScale = 0
	game.Lighting.EnvironmentSpecularScale = 0
else
	game.Lighting.EnvironmentDiffuseScale = 0.25
	game.Lighting.EnvironmentSpecularScale = 1
end
print('rendering')
for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Model") then
		repeat wait() until game.Players.LocalPlayer.Character
		char = game.Players.LocalPlayer.Character
		spawn(function()
			while wait(1) do
				if v:IsDescendantOf(char) then return end
				if (v:GetModelCFrame().Position-char:WaitForChild("HumanoidRootPart").Position).magnitude > distance then
					local block = Instance.new("Part", v.Parent)
					block.Name = "NaoRenderizado"
					block.BrickColor = BrickColor.new("Really black")
					block.CFrame = v:GetModelCFrame()
					print(v:GetModelCFrame())
					block.Size = v:GetExtentsSize()
					block.Anchored = true
					renderedObjects[v] = nil
					unrenderedObjects[v] = {["Object"] = block, ["Parent"] = v.Parent, ["Material"] = nil}
					v.Parent = nil
				else
					if unrenderedObjects[v] then
						unrenderedObjects[v]["Object"]:Destroy()
						v.Parent = unrenderedObjects[v]["Parent"]
						renderedObjects[v] = {["Object"] = v, ["Parent"] = v.Parent, ["Material"] = nil}
					end
				end
			end
		end)
	end
	if v:IsA("BasePart") and v.Name ~= "Terrain" then
		if detail then
			if v.Reflectance > 0 then
				unrenderedObjects[v] = {["Object"] = v, ["Parent"] = v.Parent, ["Material"] = v.Material, ["Reflectance"] = v.Reflectance}
				v.Reflectance = 0
			end
		else
			if unrenderedObjects[v] then
				if unrenderedObjects[v]["Reflection"] ~= 0 then
					v.Reflectance = unrenderedObjects[v]["Reflection"]
				end
			end
		end
		if (qualityLevel and qualityLevel < 3) or detail then
			if (v.Size.X < 0.5 or v.Size.Z < 0.5 or v.Size.Y < 0.5) and not v:IsDescendantOf(game:GetService("Players")) then
				if unrenderedObjects[v] then return end
				unrenderedObjects[v] = {["Object"] = v, ["Parent"] = v.Parent, ["Material"] = v.Material, ["Reflectance"] = v.Reflectance}
				v.Parent = nil
			else
				if renderedObjects[v] then return end
				renderedObjects[v] = {["Object"] = v, ["Parent"] = v.Parent, ["Material"] = v.Material, ["Reflectance"] = v.Reflectance}
			end
		else
			if unrenderedObjects[v] then
				v.Parent = unrenderedObjects[v]["Parent"]
				unrenderedObjects[v] = nil
				renderedObjects[v] = {["Object"] = v, ["Parent"] = v.Parent, ["Material"] = v.Material, ["Reflectance"] = v.Reflectance}
			end
		end
		if ((qualityLevel and qualityLevel < 2) or material) and v.Material ~= Enum.Material.SmoothPlastic then
			if not unrenderedObjects[v] then
				unrenderedObjects[v] = {["Object"] = v, ["Parent"] = v.Parent, ["Material"] = v.Material, ["Reflectance"] = v.Reflectance}
				renderedObjects[v] = nil
				v.Material = Enum.Material.SmoothPlastic
			end
		else
			if unrenderedObjects[v] then
				v.Material = unrenderedObjects[v]["Material"]
				renderedObjects[v] = unrenderedObjects[v]
				unrenderedObjects[v] = nil
			end
		end
	end
end
   end

And that’s everything I guess
As for the solutions I have tried to rewrite this 2 times so the code got even messier than the first iteration.

Please clean up your code. This is a mess and it’s hard to follow what you’re trying to do. Have you tried adding prints to see what if statements go through?

3 Likes

Normally I’d first make functional code first and then clean it(because I can’t do both at same time), but I’m stuck at the first phase.

About printing, most of the if statements go through, I have tried printing but for some reason they don’t change?

I can try making clean code right now but it wont be functional(Mark with a like if yes to prevent spam generation).

It’s a cool idea. I’d just like to plant an idea for your consideration while you wait for a solution. Things that come to mind:

  1. Something that is very specialized and requires lots of unique scripting may easily break as Roblox updates the platform. You’ll need to constantly keep up with fixes if your idea is quite specialized.

  2. The platform has these functions built in (sort of). You can verify this by loading a crowded Studio file and while playing watch the distant parts disappear and reappear.

If we were collaborating on this I’d ask: Is it worth it given the Streaming Enabled function? Is it going to break in 2 months? Do the built in platform functions interfere with it? Do you see decent performance gains or is it actually bogging things down?

Cool concept though!

StreamingEnabled is not customizable and you cannot define what should be rendered and what shouldn’t. That’s why I am making my own system.

1 Like

Update here’s my attempt at clean code

local function cover(what)
if what:IsA("Model") then
	local block = Instance.new("Part")
	block.Parent = what.Parent
	block.Anchored = true
	block.CanCollide = false
	block.BrickColor = BrickColor.new("Really black")
	block.Size = what:GetExtentsSize()
	block.CFrame = what:GetModelCFrame()
	return block,what.Parent
end
end

local function unRenderObj(obj, typea)
if not unrenderedObjects[obj] then
	if obj:IsA("BasePart") and obj.Name ~= "Terrain" and #obj:GetChildren() < 1 then -- terrain is a basepart, which is a bruh moment.
		renderedObjects[obj] = nil
		unrenderedObjects[obj] = {["Object"] = obj, ["Parent"] = obj.Parent, ["Material"] = obj.Material, ["Reflectance"] = obj.Reflectance}
		if typea == "Detail" then -- checks if object is a detail
			obj.Parent = nil
		elseif typea == "Material" then
			obj.Material = Enum.Material.SmoothPlastic
			obj.Reflectance = 0
		end
	elseif obj:IsA("Model") and not obj:FindFirstChild("Priority") then --Renders priority models, stuff that shouldn't be blocked out.
		if obj:IsDescendantOf(game.Players) then return end
		renderedObjects[obj] = nil
		spawn(function()
			while wait() do
				if (currentcamera.CFrame.Position-obj:GetModelCFrame().Position).magnitude > distance then
					local b = cover(obj)
					unrenderedObjects[obj] = {["Object"] = b, ["Parent"] = obj.Parent, ["Material"] = nil, ["Reflectance"] = nil}
					obj.Parent = nil
				else
					if unrenderedObjects[obj] then
						unrenderedObjects[obj]["Object"]:Destroy()
						obj.Parent = unrenderedObjects[obj]["Parent"]
						unrenderedObjects[obj] = nil
						renderedObjects[obj] = {["Object"] = obj, ["Parent"] = obj.Parent, ["Material"] = nil, ["Reflectance"] = nil}
					end
				end
			end
		end)
	end
end
  end

local function renderObj(obj, typea)
if unrenderedObjects[obj] then
	if obj:IsA("BasePart") then
		if typea == "Detail" then
			obj.Parent = unrenderedObjects[obj]["Parent"]
		elseif typea == "Material" then
			obj.Material = unrenderedObjects[obj]["Material"]
			obj.Reflectance = unrenderedObjects[obj]["Reflectance"]
		end
		unrenderedObjects[obj] = nil
		renderedObjects[obj] = {["Object"] = obj, ["Parent"] = obj.Parent, ["Material"] = obj.Material, ["Reflectance"] = obj.Reflectance}
	end
end
end

function render(qualityLevel)
if qualityLevel ~= nil then
	distance = qualityLevel+1*50
	local m = Instance.new("Message")
	m.Parent = workspace
	m.Text = "Utilize as configurações do jogo! Aperte o = no teclado!"
	delay(5,function()
		m:Destroy()
	end)
end
for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("BasePart") then
		if detail and (v.Size.X < 0.35 or v.Size.Z < 0.35 or v.Size.Y < 0.35) then
			unRenderObj(v, "Detail")
		else
			renderObj(v, "Detail")
		end
		if material then
			unRenderObj(v, "Material")
		else
			renderObj(v, "Material")
		end
	elseif v:IsA("Model") then
		unRenderObj(v)
	end
end
end

But now it’s giving out

[12:48:38.171 - Cannot change Parent of type Status]
1 Like

New issue(How does this keep happening?)
It just doesn’t undo any change, the Status one is because there is a Status in Humanoid that is considered a model, just changed to ClassName and it worked.

Issue still persisting, I still cannot render again models that are close, details don’t go back when I tick it, neither do reflectance/materials.