Turning all parts with a number above [X number] transparent

Hey, Gang.

I’ve got this script that changes the transparency of parts in a viewmodel, so that when the player inspects the viewmodel the amount of bullets in a magazine updates with the current mag count.

However what i’m using is kinda ineffective. I want to have it so that every part with a number above the current magazine count is turned transparent. How would i go about it?

function checkForBullets(viewmodel)
	coroutine.wrap(function()
		if viewmodel and Reloading ~= true then
			local mag = viewmodel:FindFirstChild("Bullets")
			for _, v in pairs(mag:GetDescendants()) do
				if v:IsA("BasePart") then
					if Mag < MaxAmmo then
						if v.Name == "ammo_0"..Mag + 1 or v.Name == "ammo_".. Mag+ 1 then
							v.Transparency = 1
						end
					end
					if Mag <= 0 then
						v.Transparency = 1
					end
				end
			end
		end
	end)()
end

function resetBullets(viewmodel)
	coroutine.wrap(function()
		if viewmodel then
			local mag = viewmodel:FindFirstChild("Bullets")
			for _, v in pairs(mag:GetDescendants()) do
				if v:IsA("BasePart") then
					v.Transparency = 0
				end
			end
		end
	end)()
end

You need to get only the number out of the name and compare that number to current bullet count. You can do it with string.match():

if Mag >= MaxAmmo then return end --you should check if mag is full before entering the loop
for _, v in pairs(mag:GetDescendants()) do
	if not v:IsA("BasePart") then continue end
	if Mag <= 0 then
		v.Transparency = 1
	else
		local index = tonumber(string.match(v.Name,"%d+")) --find the number in the name
		if not index then continue end --if we dont find a number skip to next bullet
		if index > Mag then --if bullet number is higher than current bullet count make it transparent
			v.Transparency = 1
		end
	end
end
1 Like

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