Error in my code (NEED HELP!)

so this code is not working as intended it breaks after it being used a few times so i have tired different ways to stop that but it still breaks even after the RestoreMaterials() function is the main problem

local plrs = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Materials = {}
local debounces = {} 
local timesfunctioncalled = 0
debounces["Busy"] = false
 function ReduceLagEffects()
	if debounces["Busy"] == true then return end
	debounces["Busy"] = true 
	for i, Part in pairs(workspace:GetDescendants()) do
		if Part:IsA("BasePart") then
			if	Part.Material == Enum.Material.Neon then 
				else
				if timesfunctioncalled > 0 then
					Part.Material = Enum.Material.SmoothPlastic
				elseif timesfunctioncalled == 0 then
			Part.Material = Enum.Material.SmoothPlastic
		        end
			end
		end
	end
	for i,tree in pairs(workspace.Folder:GetChildren()) do
		if tree	.Name == "Tree" then
			tree.Parent = game.ServerStorage
		end
	end
	timesfunctioncalled += 1
	debounces["Busy"] = false 
end

function RestoreMaterials()
	if debounces["Busy"] == true then return end
	for _, data in ipairs(Materials) do
		data.Part.Material = data.Material
	end
	
	for i,tree in pairs(game.ServerStorage:GetChildren()) do
		if tree	.Name == "Tree" then
			tree.Parent = game.Workspace.Folder
		end
	end
	debounces["Busy"] = false 
end

function ReduceLag()
	local ReduceLagValue = plrs.LocalPlayer:FindFirstChild("ReduceLag")

	if ReduceLagValue and ReduceLagValue.Value == false then
		local tween1 = TweenService:Create(script.Parent, TweenInfo.new(0.8), {BackgroundColor3 = Color3.new(1, 0, 0)})
		local tween = TweenService:Create(script.Parent.Circle, TweenInfo.new(.5), {Position = UDim2.new(0, 0, 0, 0)})
		if debounces["Busy"] == true then return end	
		debounces["Busy"] = true
		tween1:Play()
		tween:Play()
		debounces["Busy"] = false
		RestoreMaterials()
		
	elseif ReduceLagValue and ReduceLagValue.Value == true then
		script.Parent.BackgroundColor3 = Color3.new(0, 1, 0)
		local tween1 = TweenService:Create(script.Parent, TweenInfo.new(0.8), {BackgroundColor3 = Color3.new(0, 1, 0)})
		local tween = TweenService:Create(script.Parent.Circle, TweenInfo.new(.5), {Position = UDim2.new(0.704, 0, 0, 0)})
		if debounces["Busy"] == true then return end
		debounces["Busy"] = true
		tween1:Play()
		tween:Play()
		
		debounces["Busy"] = false
		ReduceLagEffects()
	end
	
	if ReduceLagValue then
		ReduceLagValue.Changed:Connect(ReduceLag)
	end
end

wait(3)
ReduceLag()

function OnClick()
	if debounces["Busy"] == true then return end
	game.ReplicatedStorage:WaitForChild("ChangeValue"):FireServer()
end

local UIS = game:GetService("UserInputService")
local toggleButton = script.Parent.Parent.Toggle

if UIS.MouseEnabled then
	toggleButton.MouseButton1Click:Connect(OnClick)
end

if UIS.TouchEnabled then
	toggleButton.TouchTap:Connect(OnClick)
end

function PartMaterials()
	for i,Part in pairs(workspace:GetDescendants()) do
		if Part:IsA("BasePart") then
			table.insert(Materials, {Part = Part, Material = Part.Material})
			task.wait(1)
		end
	end
end


PartMaterials()

if anyone knows how to fix the code please let me know (no erros,warnings)
e.wmv (841.4 KB)
robloxapp-20231127-1757245.wmv (177.3 KB)

ill be back online tmrw so sorry if i do not respond.

1 Like

This should be in #help-and-feedback:scripting-support , it’s a question about Scripting. Platform Usage Support is for generic issues/support with the website, Studio, or the Roblox application, etc.

Include screenshots of Explorer if you can and solutions you’ve tried, and explanation of the aim of the code. Read this:

What exactly are you having problems with?

1 Like

Bro said he needs help but didnt elaborate any more :skull:

1 Like

the function ReduceLagEffects() well mainly the RestoreMaterials() function as after it gets toggled a few times it stops working an your stuck without Materials

1 Like

thanks for the info i have updated it
added videos rn

1 Like

sorry ill get onto that right now.

i still need help, have you noticed why it doesnt work or anything wrong?

1 Like

I’m taking a look at it now. Is this a server or local script?

I’m assuming local as you access the player using Players.LocalPlayer and you use UserInputService. I’m not seeing anything wrong with this script, I’ll keep looking, though. Try adding print statements. I think something is off with the debounce.

It is a local script, sorry for the late response

1 Like

Yes but what? me and @DonKingFrog have already tried that

1 Like

I’ve gone ahead and organized all the functions and such into tables and such just to make it a bit more readable.

Problem :


I’ve found that the toggle was the problem. Once your spam clicked faster than the cooldown it would set the value to false but not toggle, this would then break the system entirely because it is expecting the opposite value.

Additionally, the materials were the main issue, the trees were always working if the toggle worked. Materials sometimes overwrote themselves with smooth plastic.

I’ve added the debounce to most functions and such, you should just test and rewrite certain areas to support your code.

Code :


----- || SERVICES || -----
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local TweenService = game:GetService("TweenService")

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")



----- || LIBRARY || -----
local Remove = {}; Remove.__index = Remove
local Restore = {}; Restore.__index = Restore
local Loader = {}; Loader.__index = Loader


----- || VARIABLES || -----
local Materials, Debounces, Tweens = {}, {}, {}



----- || LOADER || -----
function Loader:GetMaterials()
	coroutine.wrap(function()
		for _, part in workspace:GetDescendants() do
			if not part:IsA("BasePart") then continue end

			Materials[part] = part.Material
		end

		workspace.DescendantAdded:Connect(function(part)
			if not part:IsA("BasePart") then return end

			Materials[part] = part.Material
		end)

		workspace.DescendantRemoving:Connect(function(part)
			if not part:IsA("BasePart") then return end

			Materials[part] = nil
		end)
	end)()
end



----- || RESTORE || -----
function Restore:Materials()
	for _, part in workspace:GetDescendants() do
		if not part:IsA("BasePart") then continue end

		part.Material = Materials[part]
	end
end

function Restore:Trees()
	for _, tree in ServerStorage:GetChildren() do
		if tree.Name:lower() ~= "tree" then continue end

		tree.Parent = workspace:FindFirstChild("Folder")
	end
end



----- || REMOVE || -----
function Remove:Materials()
	for _, part in workspace:GetDescendants() do
		if not part:IsA("BasePart") then continue end

		part.Material = Enum.Material.SmoothPlastic
	end
end

function Remove:Trees()
	for _, tree in workspace:FindFirstChild("Folder"):GetChildren() do
		if tree.Name:lower() ~= "tree" then continue end

		tree.Parent = ServerStorage
	end
end



----- || UI || -----
function Toggle(bool : boolean?)
	if Debounces["ReduceLag"] then return end
	Debounces["ReduceLag"] = true	
	
	if not bool then bool = Players.LocalPlayer:FindFirstChild("ReduceLag").Value end
	if bool == nil then return end
	
	if bool then
		Restore:Materials()
		Restore:Trees()
	else
		Remove:Materials()
		Remove:Trees()
	end

	task.wait(1)
	Debounces["ReduceLag"] = false
end



----- || BACKGROUND || -----
repeat task.wait() until Players.LocalPlayer and Players.LocalPlayer:FindFirstChild("ReduceLag")

Loader:GetMaterials()

Players.LocalPlayer:FindFirstChild("ReduceLag").Changed:Connect(function()
	if Debounces["ReduceLag"] then return end
	Toggle()
end)

if UserInputService.TouchEnabled then 
	script.Parent.Parent.Toggle.TouchTap:Connect(function()
		if Debounces["ReduceLag"] then return end
		ReplicatedStorage:FindFirstChild("ChangeValue"):FireServer()
	end)
else
	script.Parent.Parent.Toggle.MouseButton1Click:Connect(function()
		if Debounces["ReduceLag"] then return end
		ReplicatedStorage:FindFirstChild("ChangeValue"):FireServer()
	end)
end
2 Likes

It works, you may of forgotten abut the tween but i have added that into it now thxs!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.