Function isn't working properly

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

Video link

1 Like

the vid of what’s happening

General things:

  1. Follow Roblox’s entire style guide for your code, but most importantly the section I linked so we can actually read it
  2. Explain the error in words and embed videos in the post. Discourse lets you upload files.
  3. I don’t have a Google account, and when I made a throwaway one, I don’t have access to the video.

Toying with your code for a bit, you might be seeing that

tweenpart:Play()
task.wait(1.1)

should be

tweenpart:Play()
tweenpart.Completed:Wait()

and I recommend adding TextTransparency = 1 to your tweeninfo. (Use a UIStroke if you want users to see where to click as the button fades.)

Pedantry
local count = 0
for _ in pairs(buttons) do
	count += 1
end

should be

local count = #buttons

local function randomattack()
	-- ...
	local random = getNewRandom(count)

should be

	local random = getNewRandom()
1 Like

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.

1 Like

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).

My full implementation

Assuming an instance structure of

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
1 Like

Thank you! Kind of a easy solution I just had to make the buttons transparency back to zero
:sweat_smile:. I also appreciate the extra feedback on my script

	chosenbutton.Visible = true
	chosenbutton.BackgroundTransparency = 0
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.