Barrier1 = game.Workspace.Barrier1
Barrier1.Transparency = 1
Barrier1.CanCollide = false
Barrier1.Anchored = true
cost = 0
script.Parent.MouseButton1Click:connect(function()
if game.Players.LocalPlayer.leaderstats.Cash.Value >= cost then
game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - cost
Barrier1.Anchored = true
local BarrierTransparency = game.Workspace.Barrier1.Transparency
repeat
Barrier1.Transparency = Barrier1.Transparency - 0.01 -- Will decrease transparency by "0.1"
wait(0.1) --Replace with a time
until Barrier1.Transparency == 0
Barrier1.CanCollide = true --Everything from this part below is not being applied and I have no idea why.
Barrier1.Transparency = 0
print("Your barrier has been completed")
else
print("Not enough cash")
end
end)
Using a repeat loop and one of the variables are not applying after the until section. Help?
1 answer
Hi, Roblox has issues when using decimal places when it tries to round to a perfect .01 or similar types of numbers. The technical details of this don't really matter to you, but essentially this means that if you're waiting to compare when the value is EXACTLY 0, it will never happen because the value is something like 0.0001 or otherwise. What you could do is use a <= check, if you wanted to do this way. However, I would suggest using the TweenService. It's very easy to use and here is the wiki page about it right here for the API http://wiki.roblox.com/index.php?title=API:Class/TweenService. Essentially you call the TweenService and call its Create function:
local TweenService = game:GetService("TweenService")
local tween = TweenService:Create()
the Create function takes in 3 parameters; 1) the object who's property you're going to tween 2) the TweenInfo of the tween (will get into this later), and 3) a table of the attributes to tween. Two of these are very simple to understand; for 1) you just put in Barrier1 and 3) you just put in {Transparency = 0} (very important that it is in a table). For TweenInfo, the API for the object is here: http://wiki.roblox.com/index.php?title=TweenInfo. It may look confusing, but it's basically just detailing HOW you want the object's property(s) to be tweened. In our case, we just care about how long it takes for the object's tween to take so we will just create a tweeninfo object with one argument, which gives it a custom time and defaults every other property of the tween.
local TweenService = game:GetService("TweenService")
local tInfo = TweenInfo.new(1)
local tween = TweenService:Create(Barrier1, tInfo, {Transparency = 0})
tween:Play()
Once the tween is created, you just start the tween with tween:Play().
This works well and condensed a lot of lines, but the tween occurs on a different thread than the script. What this means is that the script doesn't wait for the whole tween to occur before continuing onto the rest of the lines. In fact, it just moves on instantly. So you just need a wait(1). The total edited script should be something like this:
local TweenService = game:GetService("TweenService")
local tInfo = TweenInfo.new(1)
Barrier1 = game.Workspace.Barrier1
Barrier1.Transparency = 1
Barrier1.CanCollide = false
Barrier1.Anchored = true
cost = 0
script.Parent.MouseButton1Click:connect(function()
if game.Players.LocalPlayer.leaderstats.Cash.Value >= cost then
game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - cost
Barrier1.Anchored = true
local tween = TweenService:Create(Barrier1, tInfo, {Transparency = 0})
tween:Play()
wait(1)
Barrier1.CanCollide = true
print("Your barrier has been completed")
else
Hope this helped.
This discussion is preserved as it was. New answers, comments, and votes are closed.
