You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I would like to effectively measure how many times per second a user is tapping on an ImageButton on the screen. -
What is the issue? Include screenshots / videos if possible!
I already have code in place that measures clicks per second, which works correctly for PC players. But when I test the game, clicking normally from a phone makes the clicks per second measurement freak out. The label showing what the measurement is at any given time, shows that the clicks per second skyrockets into more than 10,000 CPS, which can’t be right!
-
What solutions have you tried so far?
Below is my existing code to track clicks per second.
local CPS_TEMPLATE = "(+AMOUNT clicks/sec)"
script.Parent.Cookie.MouseButton1Click:Connect(function()
if lstats ~= nil then lstats.Cookies.Value += 1 end --lstats is my variable I set elsewhere in the script to represent leaderstats for the local player.
local currentTime = tick() -- get the current time
local timeDelta = currentTime - lastClickTime -- calculate the time difference
local clicksPerSecond = math.round(1/timeDelta) -- calculate clicks per second
lastClickTime = currentTime -- update the last click time
UpdateCPS(clicksPerSecond)
end)
script.Parent.Cookie.TouchTap:Connect(function()
if lstats ~= nil then lstats.Cookies.Value += 1 end
local currentTime = tick() -- get the current time
local timeDelta = currentTime - lastClickTime -- calculate the time difference
local clicksPerSecond = math.round(1/timeDelta) -- calculate clicks per second
lastClickTime = currentTime -- update the last click time
UpdateCPS(clicksPerSecond)
end)
function UpdateCPS(amount: number)
local clicksAmount = amount - PreviousClicksAmount
PreviousClicksAmount = amount
if clicksAmount <= 0 then return end
ClicksDuringSecond += clicksAmount
script.Parent.lblCPS.Text = CPS_TEMPLATE:gsub("AMOUNT", tostring(ClicksDuringSecond))
script.Parent.lblCPS.Visible = true
task.delay(1, function()
ClicksDuringSecond -= clicksAmount
script.Parent.lblCPS.Text = CPS_TEMPLATE:gsub("AMOUNT", tostring(ClicksDuringSecond))
script.Parent.lblCPS.Visible = ClicksDuringSecond > 0
end)
end
Can anyone tell me why this doesn’t work properly on mobile, but it does on PC? I’m quite confused. Any help would be greatly appreciated, thanks!
Edit: I added the other function I was using.