FPS mode script would not work

Hello. I was gonna make a FPS mode for the low-end players, but I can’t seem to get it work.
Here is script:

local button = script.Parent

button.MouseButton1Click:Connect(function()
	for _, v in workspace:GetDescendants() do
		if v:IsA("Basepart") then
			v.Material = Enum.Material.Plastic
		end
	end
end)

Oh yea, also I want it so the material changes back to normal when I click the button again, and I want the settings saved.

You seem to have forgotten pairs(). And to return it back you’ve got to create a table and save the previous materials.

local previousMaterials = {}
previousMaterials[instance] = instance.Material --saving on a table to return it back later, of course you would loop through with this normally.

Be aware that other scripts changing the materials will cause conflict, you must also update new parts when they are added.

Still doesn’t work when I added pairs().

It looks like you did a typo on:

v:IsA("Basepart")

it needs to be:

v:IsA("BasePart") --capitalized P

If it still doesn’t work check if the event is being fired by using print.

1 Like

Does not work.
Also heres the output:


Oh yea also I made some changes in the script:

local button = script.Parent
local previousMaterials = {}
previousMaterials[instance] = instance.Material
local number = 1

button.MouseButton1Click:Connect(function()
	if number == 0 then
		number = 1
		if number == 1 then
			for _, v in ipairs(workspace:GetDescendants()) do
				if v:IsA("BasePart") then
					v.Material = Enum.Material.Plastic
			end
		end
	end
end)

you forgot to add an end lol

local button = script.Parent
local previousMaterials = {}
previousMaterials[instance] = instance.Material
local number = 1

button.MouseButton1Click:Connect(function()
	if number == 0 then
		number = 1
		if number == 1 then
			for _, v in ipairs(workspace:GetDescendants()) do
				if v:IsA("BasePart") then
					v.Material = Enum.Material.Plastic
				end
			end
		end
	end
end)

also, SmoothPlastic causes less lag than Plastic

Yea I forgot that, but I got a new error:

Players.grimmerschool2.PlayerGui.guis.settingframe.SemiYard1.LocalScript:3: attempt to index nil with 'Material'

comment out the third line of your script

local button = script.Parent
local previousMaterials = {}
--previousMaterials[instance] = instance.Material
local number = 1

button.MouseButton1Click:Connect(function()
	if number == 0 then
		number = 1
		if number == 1 then
			for _, v in ipairs(workspace:GetDescendants()) do
				if v:IsA("BasePart") then
					v.Material = Enum.Material.Plastic
				end
			end
		end
	end
end)

Did not work too.
This one is strange cause of no output.

Here’s a version that works for me:

local button = script.Parent
local previousMaterials = {}
local enabled = false

button.MouseButton1Click:Connect(function()
	if not enabled then
		enabled = true
		
		for _, v in ipairs(workspace:GetDescendants()) do
			if v:IsA("BasePart") then
				previousMaterials[v] = v.Material --save it so we can return later (has to be implemented)
				v.Material = Enum.Material.Plastic
			end
		end
	end
end)

also the version with the number variable does not work since it starts on 1 but needs to be 0 to run, and on this situation it is better to use a boolean

Thanks, but how do I make it so people can disable it by clicking it again?

Use the

table that @mniao has, loop through all the base parts and again and change the base part to the one listed in the previousMaterials table.

There are two problems with this

  1. If the part’s material is changed while the player is in FPS mode and the player disables it, the part’s material will be set to the one before the player enabled FPS mode.
  2. New parts will not be set to plastic.

But hey i made it anyway:

local button = script.Parent
local previousMaterials = {}
local enabled = false

button.MouseButton1Click:Connect(function()
	if not enabled then
		enabled = true

		for _, v in ipairs(workspace:GetDescendants()) do
			if v:IsA("BasePart") then
				previousMaterials[v] = v.Material 
				v.Material = Enum.Material.Plastic
			end
		end
	else --else if it is enabled lets disable it
		enabled = false
		
		for _, v in ipairs(workspace:GetDescendants()) do 
			if v:IsA("BasePart") and previousMaterials[v] then --check if it's a basepart and if we saved it
				v.Material = previousMaterials[v]
			end
		end
	end
end)

if you don’t plan to change materials on parts this will work just fine, you could also use DescendantAdded to fix the new parts problem

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