Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this block's transparency not changing on click?

script.Parent.ClickDetector.MouseClick:connect(function()
	local obj = script.Parent
	local transpar = obj.Transparency
	local anchr = obj.Anchored
	local cancoll = obj.CanCollide
	
	
		transpar = 0.1
		wait(0.1)
		transpar = 0.2
		wait(0.1)
		transpar = 0.3
		wait(0.1)
		transpar = 0.4
		wait(0.1)
		transpar = 0.5
		wait(0.1)
		transpar = 0.6
		wait(0.1)
		transpar = 0.7
		wait(0.1)
		transpar = 0.8
		wait(0.1)
		transpar = 0.9
		wait(0.1)
		transpar = 1
end)

I am attempting to make a block's transparency change over time on click. This isn't working for some reason and I am confused as to the reason why. I suspect that it is something with my function setup, but I am not sure. Please explain your answer, thank you.

2 answers

✓ Accepted answer

Let me show you a much easier way to do this. It uses a for loop that runs until I is equal to 1 at 0.1 intervals. Also, your variables are messed up, I'm a bit confused as to what you're trying to do when you made a variable of the objects properties but try this.

script.Parent.ClickDetector.MouseClick:connect(function()
for i = 0,1,0.1 do
wait(0.1)
obj.Transparency = i
end
end)

3 comments

0

The variable for the other object properties are for other things I'm going to be doing in the script.

0

Thank you, this helped my problem.

0

Excellent use of for loops. Bravo, yoshi!

Yoshi is correct that you should be using a for loop, but that's not why your code wasn't changing the transparency.

On line 3, you wrote

local transpar = obj.Transparency

In other words, you made a variable equal to a property.

Don't do this.

When a variable is set to a property, it simply becomes the value of that property. It retains no reference to the actual object. transpar is just a number.

local transpar = obj.Transparency
-- equivalent to;
local transpar = 0

Make your variables equal to objects. Then you can edit their properties.

obj.Transparency = 0.5

1 comment

0

Ok thanks, I fixed it.

This discussion is preserved as it was. New answers, comments, and votes are closed.