How to remember player parts colors before being changed?

Alright so here’s the code:

--[[
	@author TwinPlayzDev_YT
	@since 2/15/2021
	This script will give the player AFK Overhead GUI When they Tab Out.
--]]


--[ LOCALS ]--

local AFKStatus = {}
local AFKEvent = game.ReplicatedStorage:FindFirstChild("AFK")

--[ FUNCTIONS ]--

if not AFKEvent then
	AFKEvent = Instance.new("RemoteEvent")
	AFKEvent.Parent = game.ReplicatedStorage
	AFKEvent.Name = "AFK"
end

local updateAFK = function(player,enable)

	local char = player.Character
	local face = char:FindFirstChild("Head")

	if enable then	-- start

		for i,Child in pairs (char:GetChildren()) do
			if Child:IsA('MeshPart') then
				Child.Material = 'ForceField'
			end
		end
		--
		for i,Child in pairs (char:GetChildren()) do
			if Child:IsA('Accessory') then
				Child:FindFirstChildWhichIsA("MeshPart").Material = 'ForceField'

			end
		end

	else -- stop 

		for i,Child in pairs (char:GetChildren()) do
			if Child:IsA('MeshPart') then
				Child.Material = 'Plastic'

			end
		end
		--
		for i,Child in pairs (char:GetChildren()) do
			if Child:IsA('Accessory') then
				Child:FindFirstChildWhichIsA("MeshPart").Material = 'Plastic'

			end
		end

	end
end

AFKEvent.OnServerEvent:Connect(function(player,enable)
	AFKStatus[player] = enable
	updateAFK(player,enable)

end)

This is a AFK system that I’ve been using in my game, if the player tabs out it turns them into a forcefield ( or whatever is inside the script ) then if they tab in it turns them back to normal.

And pretty much I want to do is make it so when the player parts and meshes n stuff turn into a forcefield they turn into a specific color but then changing the color back to whatever it was before if the player tabs back in

1 Like

Perhaps you can store the part’s colors in a dictionary table before you change the character’s color? Like when they click, store the original color before the code that changes it.

1 Like

@MykoNil’s idea will work but if it’s just one color you need to remember you can also just assign it to a variable. But if you need to store multiple do what Myko said.

1 Like

usually players have their skin color set for their whole body, however others don’t and sometimes they have their head set to black and the rest white or other color, etc, I’ll try doing what Myko said and see if it works, incase yall do find an already made or more simplified version of this do tell, saves me a lot of time ( noobie scripter here lol )

I have not tried this specifically, but I imagine you could get the children of player.Character and iterate through with a for ... in pairs() loop. I.e.: (for index, bodyPart in pairs(player.Character:GetChildren().) Just checking if it’s a UnionOperation or BasePart, or something similar to avoid trying to change a value that doesn’t exist, for example, Color inside of a Humanoid.

https://developer.roblox.com/en-us/api-reference/class/BasePart
https://developer.roblox.com/en-us/api-reference/class/UnionOperation

2 Likes

you can basically store the BodyColors instance that is in the player character in somewhere before changing it if you didnt know there is an object called BodyColors that is used by humanoids to give them color

2 Likes

Store it as an attribute to the Player Instance.

Player:SetAttribute("Color", someColor3Value)
1 Like

How can I grab the current Color3 value from the player?, i know where but how do I save it in the script :I?

1 Like

how can I get the value and save it tho?

1 Like

theres properties like

["Body Colors"].RightArmColor3

then store (set whatever variable = to the color3 object) the entirety of the color3 into the specified dictionary or variable and then when you are ready to fetch it do

["Body Colors"].RightArmColor3 = Color3.FromRGB(StoredRightArm[1],StoredRightArm[2],StoredRightArm[3])
2 Likes

this works for me, thank you lots, I was very confused about all this information this is way more simplified :+1:

1 Like