Need help to change material with a command

What I want to do is that when I write “/material” and then write a material in the chat, it changes the material to my avatar, but I don’t know how to detect the materials.


local cmds = {
playerMaterial = "/material",
}

game.Players.PlayerAdded:Connect(function(plr)
	-- Commands
	plr.Chatted:Connect(function(msg)
		if msg:sub(1,cmds.playerMaterial:len()):lower() == cmds.playerMaterial:lower() then -- Not relevant
			local mat = msg:sub(cmds.playerMaterial:len()+1)--:lower() -- Detects the second word.
			print(mat) -- prints the second word the player chatted (Material)
			if Enum.Material:FindFirstChild(mat) then -- THIS IS MY DOUBT, AND I NEED HELP FOR THIS
				print("material detected")
				for _,v in pairs(plr.Character:GetDescendants()) do
					if v:IsA("MeshPart") or v:IsA("BasePart") then v.Material = tostring(mat) end
				end
			end
		end
	end
end

Really need help to detect if the second player is a material.

Everything looks fine, what is the problem that is occurring in your script?

local cmds = {
	playerMaterial = "/material",
}

game.Players.PlayerAdded:Connect(function(plr)
	-- Commands
	plr.Chatted:Connect(function(msg)
		if msg:sub(1,cmds.playerMaterial:len()):lower() == cmds.playerMaterial:lower() then -- Not relevant
			local mat = msg:sub(cmds.playerMaterial:len()+1):gsub('%s+','')--:lower() -- Detects the second word.
			print(mat) -- prints the second word the player chatted (Material)
			
			local Exist,material=pcall(function()return Enum.Material[mat]end)
			print(Exist,material)
			
			if Exist then
				print("material detected")
				for _,v in pairs(plr.Character:GetDescendants()) do
					if v:IsA("MeshPart") or v:IsA("BasePart") then v.Material = material end
				end
			end
		end
	end)
end)
1 Like

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