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
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.