Cross check IntValue in parts using a script

I wanted have a script when i touch the part in a folder it makes another part visible and this would serve as the base to a tycoon system. Currently, I loop through a folder and run a function to check if the part is touched and if it is it makes the transparency 0 and CanCollide true.

I want to see if I can give each of them an IntValue so even if the parts are in two separate folders I could “link” them with an IntValue so that when the specific part I touch has an IntValue of “1” and the part that I want visible has an IntValue of “1” it would match and only that part would be visible.

I’m also not sure if this causes lag since it would have to loop thru a large amount of parts however I’m open to hearing the feedback.

--scripted by sandy7512
local startTime = os.time()

local mainGame = game.Workspace:WaitForChild("scriptedparts")
local purchasePart = mainGame:WaitForChild("purchaseParts"):FindFirstChildWhichIsA("BasePart")
local partsChanged = mainGame:WaitForChild("partsChanged")


for _, v in partsChanged:GetChildren() do
    if v:IsA("BasePart") then
        v.BrickColor = BrickColor.new("Bright red")
        v.Transparency = 1
        v.CanCollide = false
    end
end

local function checkChanged()
    for _, v in partsChanged:GetChildren() do
        if v:IsA("BasePart") then
            v.BrickColor = BrickColor.new("Bright blue")
            v.Transparency = 0
            v.CanCollide = true
        end
    end
end

-- create a function that checks IntValues between purchasedParts and partsChanged



--debounce check
local partIsTouched = false
local function touchFunc(otherPart)
    if not partIsTouched then
        partIsTouched = true
        if otherPart.Parent:FindFirstChild("Humanoid") then
            checkChanged()
        end
        task.wait(2)
        partIsTouched = false
    end
end

purchasePart.Touched:Connect(touchFunc)

--load time check
print("PurchasePartServer loaded in", os.time() - startTime, "seconds")

I recommend using CollectionService. I would, instead of the IntValues, model the touched part and the part that’s supposed to appear together and then when the part is touched it shows all the children of its parent, except itself. Something like this:

local CollectionService = game:GetService("CollectionService")

local touchableParts = CollectionService:GetTagged("TouchablePart")

for _, part: BasePart in touchableParts do
	part.Touched:Connect(function(otherPart)
		if otherPart.Parent:FindFirstChildWhichIsA("Humanoid") then -- So that we know a player has touched it
			for _, v in part.Parent:GetChildren() do
				if v:IsA("BasePart") then
					v.Transparency = true
					v.CanCollide = true
				end
			end
			part:Destroy()
		end
	end)
end

Though you will have to tag all the touchable parts manually

How do I navigate into the folders I have in the workspace where the parts are stored?

You won’t need the folders at all. Just make sure every pair of parts that you wanted to set the same IntValues to, have the same parent. You can use a Model or a Folder for that.

So just to confirm, I just have the part I want to appear after the parent part is touched, and make sure to tag all of them with “TouchablePart” which is what I manually have to tag the parts with.

It will look something like this:
image
or this:
image
Where only the Touchable Part has the “TouchablePart” tag (or whatever you want to name it)

Oh I see, thanks for the help.

1 Like

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