Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago
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

Log in to vote
1
Answered by
yoshiegg6 176
9 years ago

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)
0
The variable for the other object properties are for other things I'm going to be doing in the script. dirty_catheter 7 — 9y
0
Thank you, this helped my problem. dirty_catheter 7 — 9y
0
Excellent use of for loops. Bravo, yoshi! yumtaste 476 — 9y
Ad
Log in to vote
5
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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
0
Ok thanks, I fixed it. yoshiegg6 176 — 9y

Answer this question