I’m sorry this code is a bit long, but here is my issue.
Whenever I run this code, one of two things happen:
(With Wait)The code will run repeatedly, causing the script to take away more money from the player than it should
(Without Wait) The code will run once, but delete the button instantly, causing no animations to play.
I do not know how else I would make it run ONLY once while still playing all of the animations, other than the debounce method, which doesn’t seem to be working.
Here is my script:
local DB = false
touched = v.Button.Touched:Connect(
function(Hit)
print(v.Price.Value)
if DB == false then
DB = true
if v.Button.CanCollide == true then
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and OwnerValue.Value == Player then
if Player.leaderstats.Cash.Value >= v.Price.Value then
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - v.Price.Value
if bparts:IsA("BasePart") then
local FadeOut = TweenS:Create(bparts, tweenFade, partPropsOut)
FadeOut:Play()
bparts.CanCollide = true
if bparts.Name == "Button" then
bparts.ButtonBuy:Play()
bparts.PriceGUI.Enabled=true
bparts.partem.Enabled = true
end
end
Objects[v.Object.Value].Parent = Purchases
wait(1) -- here
v:Destroy()
db2 = false
end
end
end
end
DB = false
end
end)
This is not the whole script, only the piece I believe to have the issue. If you need the whole script, just tell me and I will send it. Sorry if I missed any information. Thank you in advance!
I’m assuming that this is supposed to only run once, so there’s no reason to set DB back to false.
This should fix the issue of it running multiple times.
Didn’t work. I think the DB might not be actually checking. I just ran a print for DB and it did print, I think it somehow didn’t actually check for the DB.
Yes, I did remove the line, but it still kept calling. I put in a print(DB) and it printed true 12 times. I just recorded a GIF. Give me a second and I’ll have one showing it take money too. The first value prints the price of the item, the second is the value of DB.
for i, v in pairs(Buttons:GetChildren()) do
local Object = Purchases:FindFirstChild(v.Object.Value)
if Object then
Objects[Object.Name] = Object:Clone()
Object:Destroy()
end
for _, bparts in ipairs(v:GetDescendants()) do
--tween
local tweenFade = TweenInfo.new(
1,
Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out,
0,
false,
0
)
local partPropsOut = {
Transparency = 1
}
local partPropsIn = {
Transparency = 0
}
if v:FindFirstChild("Dependancy") then
coroutine.resume(coroutine.create(function()
if bparts:IsA("BasePart") then
bparts.Transparency = 1
bparts.CanCollide = false
if bparts.Name == "Button" then
bparts.PriceGUI.Enabled = false
bparts.partem.Enabled = false
end
end
if Purchases:WaitForChild(v.Dependancy.Value) then
if bparts:IsA("BasePart") then
local FadeIn = TweenS:Create(bparts, tweenFade, partPropsIn)
FadeIn:Play()
bparts.CanCollide = true
if bparts.Name == "Button" then
bparts.PriceGUI.Enabled=true
bparts.partem.Enabled = true
end
end
end
end))
end
local DB = false
touched = v.Button.Touched:Connect(
function(Hit)
print(v.Price.Value)
if DB == false then
DB = true
if v.Button.CanCollide == true then
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and OwnerValue.Value == Player then
if Player.leaderstats.Cash.Value >= v.Price.Value then
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - v.Price.Value
if bparts:IsA("BasePart") then
local FadeOut = TweenS:Create(bparts, tweenFade, partPropsOut)
FadeOut:Play()
bparts.CanCollide = true
if bparts.Name == "Button" then
bparts.ButtonBuy:Play()
bparts.PriceGUI.Enabled=true
bparts.partem.Enabled = true
end
end
Objects[v.Object.Value].Parent = Purchases
wait(1) -- here
v:Destroy()
end
end
end
end
print(DB)
--DB = false
end
end)
end
end
Hmm, I’ve gotta go right now, I’ll be back in roughly 1 hour.
It has something to do with repeating the connection to part.Touched on for the same button 13 times.
The cost seems to get printed with an increment of 13 as well so it’s solidifying my suspicion.
For each of the objects you are making appear for the dropper, you are creating a new .Touched function on the button.
local DB = false
local function OnButtonTouch(Hit, v)
print(v.Price.Value)
if DB == false then
DB = true
if v.Button.CanCollide == true then
if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player and OwnerValue.Value == Player then
if Player.leaderstats.Cash.Value >= v.Price.Value then
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - v.Price.Value
if bparts:IsA("BasePart") then
local FadeOut = TweenS:Create(bparts, tweenFade, partPropsOut)
FadeOut:Play()
bparts.CanCollide = true
if bparts.Name == "Button" then
bparts.ButtonBuy:Play()
bparts.PriceGUI.Enabled=true
bparts.partem.Enabled = true
end
end
Objects[v.Object.Value].Parent = Purchases
wait(1) -- here
v:Destroy()
end
end
end
end
print(DB)
--DB = false
end
end
for i, v in pairs(Buttons:GetChildren()) do
local Object = Purchases:FindFirstChild(v.Object.Value)
if Object then
Objects[Object.Name] = Object:Clone()
Object:Destroy()
end
for _, bparts in ipairs(v:GetDescendants()) do
--tween
local tweenFade = TweenInfo.new(
1,
Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out,
0,
false,
0
)
local partPropsOut = {
Transparency = 1
}
local partPropsIn = {
Transparency = 0
}
if v:FindFirstChild("Dependancy") then
coroutine.resume(coroutine.create(function()
if bparts:IsA("BasePart") then
bparts.Transparency = 1
bparts.CanCollide = false
if bparts.Name == "Button" then
bparts.PriceGUI.Enabled = false
bparts.partem.Enabled = false
end
end
if Purchases:WaitForChild(v.Dependancy.Value) then
if bparts:IsA("BasePart") then
local FadeIn = TweenS:Create(bparts, tweenFade, partPropsIn)
FadeIn:Play()
bparts.CanCollide = true
if bparts.Name == "Button" then
bparts.PriceGUI.Enabled=true
bparts.partem.Enabled = true
end
end
end
end))
end
v.Button.Touched:Connect(function() --there's the touch function!
OnButtonTouch(Hit, v)
end)
end
end
Actually, no. It only has called much more times than the original, but thats probably because the second button didnt work at all. Also its not the fact you forgot Hit in your function. I already noticed that.
Thanks for the Idea! I think it’s working now, all of the buttons are doing what I wanted them to. Seems like you fixed more of the issues I was having than one. Thanks man!