Color3:Lerp Expected(String got Color3)?

I’m trying to make a dynamic bone life indicator using color to represent the health.

When i try to run the code, it sometimes says this : invalid argument #2 (string expected, got Color3). The code use : v[v.Color] where v is the looped part and the second v is to reference the bone which has the same name as v(Instance) and is a child of v.

I tried looking in the dev hub but there where nothing.

local Childrens = script.Parent:GetDescendants()
local MinForce = 25
local BoneMinForce = 30
local Parts = {}

for i, v in pairs(Childrens) do
	if v:IsA("PVInstance") and not v:IsA("Model") and v.Name ~= "HumanoidRootPart" then
		Parts[v] = {500, 1000}
		v.Touched:Connect(function(Hit)
			local AppliedForce = Vector3.new(math.abs(Hit.AssemblyLinearVelocity.X - v.AssemblyLinearVelocity.X), math.abs(Hit.AssemblyLinearVelocity.Y - v.AssemblyLinearVelocity.Y), math.abs(Hit.AssemblyLinearVelocity.Z - v.AssemblyLinearVelocity.Z))
			if AppliedForce.X >= MinForce or AppliedForce.Y >= MinForce or AppliedForce.Z >= MinForce then
				local df = Vector3.new(Hit.Position.Z - v.Position.Z, Hit.Position.Y - v.Position.Y, Hit.Position.X - v.Position.X).Unit
				local Normal = math.acos((Hit.Position:Dot(df)) / (Hit.Position.Magnitude * df.Magnitude))
				local Side = Vector3.FromNormalId(Normal)
				AppliedForce = (AppliedForce.X * Side.Z) + (AppliedForce.Y * Side.Y) + (AppliedForce.Z * Side.X)
				if AppliedForce >= MinForce then
					local AppliedStress = AppliedForce * AppliedForce / 5
					Parts[v][1] -= AppliedStress
					if AppliedForce >= BoneMinForce then
						local AppliedStress = AppliedForce * AppliedForce / 2
						Parts[v][2] -= AppliedStress
						--Error at v[v.Color] ---> v[v] = v[v.Color]:Lerp(Color3.fromRGB(196, 40, 28), (1000 - Parts[v][2]) * 100 / 500 / 500)
					end
					v.Color = v.Color:Lerp(Color3.fromRGB(196, 40, 28), (500 - Parts[v][1]) * 100 / 500 / 500)
					if Parts[v][1] <= 0 then
						local D = v:Clone()
						D.Parent = workspace
						v:Destroy()
						local Sound = script.Limb:Clone()
						Sound.Parent = v
						Sound.Playing = true
					end
				end
			end
		end)
	end
end

I really confused about why it is doing that so it would be great someone found a fix.

Maybe I’m wrong but I think the reason you’re experiencing this is because of how you’re indexing:

Essentially, if we break it down into tables which is the best way to show it:

local part = {
    Color = Color3.new(1,1,1)
}

therefore, it makes sense that it would be erroring because it’s like indexing a table with an index that is of type Color3:

local colour = Color3.new(1,1,1)
local thing = part[colour]

thing would be nil because there is no index which is the same as the colour variable.
To correctly index it, we need to do part.Color, not part[colour], or in your case,

-- if v is a BasePart, then the BasePart doesn't have an index which is a Color3, only a value which is a Color3 and an index which is a string, therefore this should work:
 v.Color:Lerp(Color3.fromRGB(196, 40, 28), (1000 - Parts[v][2]) * 100 / 500 / 500)

I found the issue. It was that when i did this v[v].Color the second v was an instance and not a string so i did tostring(v) and it worked!!! But thanks anyways.

1 Like