Why am I getting two seperate results with Brick.Transparency and a variable of it?

Hello, I’m new to scripting and trying to understand things I don’t fully understand in Lua.

I’m currently stuck, as I have made two separate bricks. One has the transparency stored in the variable, while the second one doesn’t. Why is this happening?

v First Example v - This one half-works, where it only shows the “brickTransparency” going up, but that’s it. The visibility of the brick will remain the same.

local Brick = script.Parent
local brickTransparency = Brick.Transparency
local looping = true

local function IWasTouched(Part)
	local partParent = Part.Parent
	local Robloxian = Part.Parent:FindFirstChild("Humanoid")
	if Robloxian and brickTransparency < 1 then
		print("WE'RE AT THE START")
		while looping and brickTransparency < 1 do
			looping = false
			
			print("Looping")
			brickTransparency = brickTransparency + 0.1
			print(brickTransparency)
			wait(0.5)
			looping = true
		end
	end
end

Brick.Touched:Connect(IWasTouched)

v Second Example v - This one works perfectly fine. Upon being touched it will show the transparency of the block while gradually disappearing

local Brick = script.Parent
local brickTransparency = Brick.Transparency
local looping = true

local function IWasTouched(Part)
	local partParent = Part.Parent
	local Robloxian = Part.Parent:FindFirstChild("Humanoid")
	if Robloxian and Brick.Transparency < 1 then
		print("WE'RE AT THE START")
		while looping and Brick.Transparency < 1 do
			looping = false
			
			print("Looping")
			Brick.Transparency = Brick.Transparency + 0.1
			print(Brick.Transparency)
			wait(0.5)
			looping = true
		end
	end
end

Brick.Touched:Connect(IWasTouched)
2 Likes

Its because that in the first example you are storing the current value of the transparency of the brick:
local brickTransparency = Brick.Transparency
brickTransparency = brickTransparency + 0.1

So when you grab that variable brickTransparency and change it, you are not changing the property of the part, you are changing a value number that is inside that variable.

The second example works cause you are changing the property of the brick by using that variable Brick that holds the reference of the real part, not just a number:
Brick.Transparency = Brick.Transparency + 0.1

1 Like

I see now, thank you for the response.

Is there a way for me to put the actual transparency property of the brick into a variable?

Could be a way to “store” more like a “get” a variable that could hold the property. I honestly never tried cause I dont find that could be useful in any way, depending on the complexity of the project I think…

You could have a function that returns the property stored into a variable, but seems not real difference when working with that. You could use OOP to have custom functions to handle that too, but its going overcomplicated if you are starting with Lua.

Depends on the scenario but I would suggest keep using a reference of the part in the variable and then change the property of that variable Brick.Transparency += 0.1

1 Like

I’ll do just that, keeping a variable of the part I’m using and just change it from that. Thank you for the explanation.

2 Likes

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