Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Question/Issue With Multiple Assignment of Variables in One Line?

Hi all! I've been trying to script a tool that does two things when activated: changes color from red to green (and vice versa) and changes the transparency of all of the player's character's body parts, excluding the face decal, from 0 to 1 (and vice versa). Originally, in order to do this, I thought that I could have two variables that store a Color3 for green and the integer 1. Then, I would change the Color property of the tool (which is just a part named "Handle") to the value in the color variable, and change the value of the color variable to the value of the color of the tool. This works perfectly fine. The problem arises when I try to do the same for the transparency of the player's character's body parts.

Here is the original script I used:

local tool = script.Parent
local handle = tool.Handle
local storedColor = Color3.fromRGB(0,255,0)
local storedTransparency = 1

tool.Activated:Connect(function()
	handle.Color, storedColor = storedColor, handle.Color
	for I,V in pairs(tool.Parent:GetChildren()) do
		if V.ClassName == "MeshPart" or V.ClassName == "Part" and V.Name ~= "HumanoidRootPart" then
			V.Transparency, storedTransparency = storedTransparency, V.Transparency
		end
	end
end)

This is what happens when I click once: https://imgur.com/a/wfHhlEP

I figured that this was just a problem with how Roblox handled the multiple assignment for transparency, so I changed it to some if statements:

local tool = script.Parent
local handle = tool.Handle
local storedColor = Color3.fromRGB(0,255,0)

tool.Activated:Connect(function()
	handle.Color, storedColor = storedColor, handle.Color
	for I,V in pairs(tool.Parent:GetChildren()) do
		if V.ClassName == "MeshPart" or V.ClassName == "Part" and V.Name ~= "HumanoidRootPart" then
			if V.Transparency == 0 then
				V.Transparency = 1
			else
				V.Transparency = 0
			end
		end
	end
end)

This fixed the problem (as can be seen here: https://imgur.com/a/preavKB) but the addition of these lines personally bothers me, I would prefer doing multiple assignment. Does anyone know whether there was something wrong with my original script with multiple assignment, or whether multiple assignment is simply not possible in this scenario?

Thanks in advance!

1 answer

I'm not sure if this is what you meant:

local tool = script.Parent
local handle = tool.Handle
local redColor = Color3.fromRGB(255,0,0)
local greenColor = Color3.fromRGB(0,255,0)
local Activated = false
local transparencyNumber
local newColor

tool.Activated:Connect(function()
	if Activated == false then
		transparencyNumber = 1
		newColor = greenColor
	else
		transparencyNumber = 0
		newColor = redColor
	end
	Activated = not Activated --Makes activated the opposite of itself(if it were true it would become false, if false it would become true)
	handle.Color = newColor
	for I,V in pairs(tool.Parent:GetDescendants()) do --we use GetDescendants so we can reach parts such as the face, which is parented to the character's head)
		if v:IsA("BasePart") or v:IsA("Decal") then --IsA checks the part's classname, parts, unions and meshparts are all BaseParts, checking if v is a decal means we can also make the face transparent
			v.Transparency = transparencyNumber
		end
	end
end)

Sorry if I made any typos.

This discussion is preserved as it was. New answers, comments, and votes are closed.