I’m trying to make a quicktime event that’ll pick a button at random then it’ll make it visible and if the player doesn’t click it in time it’ll fail but when ever I click the button when its about to fail afterwards the chosen buttons begin to fail at a rapid pace before their timer runs out
can someone tell me what’s going wrong
local attacknum = 100
local Numofattacks = 0
local chosenbutton
local clickConnection
local loop
local connection = {}
local random = 0
local lastRandom = 0
local clicked = false
local functionactive = false
local Strikegui = script.Parent
local Bpdam = game.ReplicatedStorage:WaitForChild("REstorage").bodypartRE.bodypartdamange
local attackpercent = 0
local runservice = game:GetService("RunService")
local tween = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(1)
local buttonframe = Strikegui:WaitForChild("buttonframe")
local buttons = {
Button1 = buttonframe:WaitForChild("Button1"),
Button2 = buttonframe:WaitForChild("Button2"),
Button3 = buttonframe:WaitForChild("Button3"),
Button4 = buttonframe:WaitForChild("Button4"),
Button5 = buttonframe:WaitForChild("Button5")
}
local count = 0
for _ in pairs(buttons) do
count += 1
end
local function getNewRandom()
local random = math.random(1, count)
while random == lastRandom do
random = math.random(1, count)
end
lastRandom = random
return random
end
local function randomattack()
if functionactive then
print("didn't fire") return
end
functionactive = true
if chosenbutton and clickConnection then
chosenbutton.Visible = false
clickConnection:Disconnect()
clickConnection = nil
end
local random = getNewRandom()
chosenbutton = buttons["Button"..random]
chosenbutton.Visible = true
clickConnection = chosenbutton.MouseButton1Click:Connect(function()
if clicked then return end
clickConnection:Disconnect()
clicked = false
chosenbutton.Visible = false
Numofattacks += 1
print("Attack successful! Turn:", Numofattacks)
if Numofattacks < attacknum then
functionactive = false
randomattack()
return
elseif Numofattacks >= attacknum then
print("All attacks used!")
chosenbutton.Visible = false
clickConnection:Disconnect()
end
end)
local tweenpart = tween:Create(chosenbutton,tweeninfo,{BackgroundTransparency = 1})
tweenpart:Play()
tweenpart.Completed:Wait()
if chosenbutton.BackgroundTransparency == 1 and clicked == false then
print("Failed attack! Turn skipped.")
print(Numofattacks)
clicked = false
chosenbutton.Visible = false
Numofattacks += 1
if Numofattacks < attacknum then
functionactive = false
randomattack()
if clickConnection then
clickConnection:Disconnect()
return
end
else
print("All attacks used!")
end
end
end
I’ve changed me script to be more readable for what i’ve read quickly, I edited the post to explain the problem to my best ability and I also made the video public to see.
By the way, I tried to implement something like this myself to see where you might be having trouble, and I may have found it, but I don’t think it’s related to the timing:
Your random number generator may be generating button indices that have already been tweened out of visibility. I just made a hashmap attacked = {} where keys are button numbers and values are booleans, i.e. attacked = {[1] = false, [2] = true, [3] = false, [4] = true, [5] = true} to constrain further the valid random numbers (here only 1 or 3).
local TweenService = game:GetService("TweenService")
local tweenDelay = TweenInfo.new(1.5)
local function attack(button: TextButton): boolean
local tween = TweenService:Create(button, tweenDelay, {BackgroundTransparency = 1, TextTransparency = 1})
tween:Play()
local clicked = false
local connection = button.Activated:Connect(function()
clicked = true
tween:Cancel()
button.Visible = false
end)
tween.Completed:Wait() --Breaks yielding when `tween:Cancel()` is called
tween:Destroy()
connection:Disconnect()
return clicked
end
local attacked = {}
local selected = nil
local buttons = {}
for _, child in script.Parent.buttonframe:GetChildren() do
if child:IsA("TextButton") and child.Name:sub(1, 6) == "Button" then
buttons[tonumber(child.Name:sub(-1, -1))] = child
end
end
for _ = 1, #buttons do
-- Make sure the button selected hasn't been yet
repeat
selected = math.random(1, #buttons)
until not attacked[selected]
attacked[selected] = true
-- This function call can be used as a condition to `if`
-- to detect whether the player hit the button on time
attack(buttons[selected])
end