Part only changes transparency SOMETIMES

I’m making a tycoon game and I’m trying to make a function that turns all the bought stuff visible after they are bought by the button. For some reason this works, but sometimes it just randomly doesn’t, and I have been trying to find out why for about 3 hours. If anyone can help, please do. Thanks!

**

Videos of it not working

robloxapp-20231111-1409573.wmv (2.3 MB)
robloxapp-20231111-1409362.wmv (1.6 MB)

**

Script Context:
Unlocks is a folder that the button unlocks when bought
This script is in ServerScriptService and gets all buttons tagged as Button

Script

local cs = game:GetService("CollectionService")
local MakeDropper = game:GetService("ReplicatedStorage"):WaitForChild("Droppers")
local MakeDropper = require(MakeDropper)
local ts = game:GetService("TweenService")


wait(5)--waits for stuff to load in
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------PressurePlateButtons-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function PressurePlateButtons(v)--Finds all the buttons in workspace
	if v.Name ~= "StartButton" then
		v.Transparency = 1
	end

	local pressurePlate = v
	local base = pressurePlate:FindFirstAncestor("BASE")

	local droppers = {}
	local transparencies = {}

	local function fadeTween(part, goal, Time)
		ts:Create(part, TweenInfo.new(Time, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), goal):Play()
	end


	local function vis(parent, transparency, Time, onlyInUnlocks, invLabel)
		for i, childrenOfBought in ipairs(parent:GetChildren()) do
			wait()
			if childrenOfBought.Name ~= onlyInUnlocks then 
				if turnInVis == 1 then--invisible
					local s, _ = pcall(function()
						
						transparencies[childrenOfBought] = childrenOfBought.Transparency
						repeat wait() until transparencies[childrenOfBought] == childrenOfBought.Transparency
						wait(0.05)
						transparencies[childrenOfBought] = childrenOfBought.Transparency

						coroutine.wrap(fadeTween)(childrenOfBought, {Transparency = turnInVis}, Time)
					end)


				else--visible
					local _, fail = pcall(function()
						coroutine.wrap(fadeTween)(childrenOfBought, {TextTransparency = invLabel}, Time)
					end)
					if fail then
						print(transparencies[childrenOfBought])
						if transparencies[childrenOfBought] then
							coroutine.wrap(fadeTween)(childrenOfBought, {Transparency = transparencies[childrenOfBought]}, Time)
						end
					
					end

				end

				vis(childrenOfBought, turnInVis, Time, "Unlocks", invLabel)
			end
		end
	end



	--FindDroppers
	for i, v in pairs(pressurePlate.Unlocks:GetChildren()) do
		if (v.Name:lower():match(("Dropper"):lower())) and not v:HasTag("Button") then
			table.insert(droppers, v)

		end
	end

	vis(pressurePlate.Unlocks, 1, 0, "GoThroughAllUnlocks", 0)

	local touched = false
	local c
	c = pressurePlate.Touched:Connect(function(toucher)
		if touched == true then return end
		if not toucher.Parent:FindFirstChild("Humanoid") then return end
		local char = toucher.Parent
		local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
		if pressurePlate.Transparency == 1 then return end
		if not plr then return end
		touched = true
		c:Disconnect()


		print("I have been touched by your kids", pressurePlate)
		pcall(function()pressurePlate.Head:Destroy() pressurePlate.BillboardGui.TextLabel.TextTransparency = 1 end)


		fadeTween(pressurePlate, {Transparency = 1}, 1)
		--fadeTween(pressurePlate.BillboardGui.TextLabel, {TextTransparency = 1}, 1)

		print(pressurePlate.Unlocks)

		vis(pressurePlate.Unlocks, 0, 1, "Unlocks", 0)



		for i, dropper in pairs(droppers) do
			local Type, IndexNum = MakeDropper.FindType(dropper)
			local dropperTable = MakeDropper.NewDropper(dropper, Type, base, toucher.Parent)
			if dropperTable then
				dropperTable:Drop()
			end
		end

	end)	
end







for i, v in pairs(cs:GetTagged("Button")) do
	task.wait(.1)
	PressurePlateButtons(v)



end
1 Like