How to make clicks per second like this video?

Edit: Everyone that tried to help on the old post, I am completely sorry. I thought the script found on the stackoverflow was the solution to the problem I’ve been having… which was not. Again I’m sorry.

The thing I was trying to do is in this video, check how per second goes up when the person taps, and how it goes down when he doesn’t.

Old post

Anyone know how to translate this c++ to lua

I’m trying to make an average click per second script and I ran across this stackoverflow question. And there seems to be an answer except its in c++ which I don’t know how to read.

const unsigned BUFFER_SIZE = 30;
unsigned counters[BUFFER_SIZE];
unsigned current = 0;
time_t last;

void init() {
    time(&last);
}

void update() {
    time_t now;
    time(&now);
    while (now - last >= 1) {
        ++last;
        current = (current+1)%BUFFER_SIZE;
        counters[current] = 0;
    }
}

void mousePressed() {
    ++counters[current];
}

float average() {
    float sum = 0;
    for (int i = 0; i < BUFFER_SIZE; ++i) {
        sum += counters[i];
    }
    return sum/BUFFER_SIZE;
}

I could try and read it, but I really don’t think I can do well at it.

Edit: Actually i’ll try and see if I can read it later(I’ll prob remove this post if I figure it out and there’s no reply.)

Why do you want to translate this anyway? Only if you know both C++ and Lua language, you can translate it.

“I’m trying to make an average click per second script”.

It wouldn’t be that hard to code this in lua. When you say clicks per second, do you mean clicks in general, clicks on a text button, clicks on a click detector?

Clicks in general. And no I’m not making a clicks per second script I’m making an average clicks per second script.

Do you want to display this on a text button or something? Something like this on a local script would work:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local textlabel = --define here

local start = tick()

local clicks = 0

mouse.Button1Up:Connect(function()
      clicks += 1
end)

while wait(1) do
      local timeSinceStart= math.floor(tick() - start)
     print("Average Clicks Per Second: "..math.floor(clicks/timeSinceStart * 100)/100)
end
2 Likes

Yes I’m trying to display it on a textlabel.

Edit: I cant try it right now but I’ll see if it works later.

It works on my end. When I started clicking the number increased, when I stopped, the number started falling again.

1 Like

It wasn’t quite what I was looking for… but I mean it does answer my question so i’ll mark it.

I’ll see if I can modify it to go faster

I see what you mean. You don’t have to mark mine as the solution because the whole point is to help you, so if it isn’t what you are looking for then it’s not really the solution. I gtg for a bit but i’ll get back to you when I have a solution.

1 Like

Oh… you saw it. Alright then.

No idea how much this will help but, GitHub - Oberon00/apollo: Convert anything from C++ to Lua and back, easily create modules & more.

Haven’t tested but something like this should work, just connect OnMouseButton1Down to something and call Average() whenever you want the average.

local BUFFER_SIZE = 30; -- seconds to get running average for

local counters = table.create(BUFFER_SIZE, 0)
local current = 1; -- lua is 1-indexed

local last = time(); -- init() equivallent

-- update() equivallent
-- could use Stepped or Heartbeat here too but whatever
game:GetService("RunService").RenderStepped:Connect(function()
	local now = time()
	
	while now - last >= 1 do
		last += 1
		current = (current % BUFFER_SIZE) + 1 -- lua is 1-indexed
		counters[current] = 0;
	end
end)

local function OnMouseButton1Down()
	counters[current] = counters[current] + 1;
end

local function Average()
	local sum = 0
	for i = 1, BUFFER_SIZE do
		sum += counters[i];
	end
	return sum / BUFFER_SIZE
end
3 Likes

It’s similar to the one XdJackyboiiXd21 except it’s a little slower.

His gives you the average clicks per second for the total runtime of the game. Mine gives the average clicks per second for just the last 30 seconds, which is what the code you posted also does. Not sure what you’re looking for if not that :slight_smile:

Yeah, I couldn’t really word it in a way that everyone could understand so yes techically you’re right. What I was trying to do is similar to what’s happening on this one.

Notice how the per second goes up whenever the guy starts clicking, and how it starts down when he stops.

Edit: it’s in desc.

Yes, that’s what the code you posted (and I translated) does. Maybe you just need to decrease the buffer size to like 5.

1 Like

Dang… why didn’t I think of that. I spent 4 hours creating some codes that I could’ve solved just by changing a single value from that script. oml

Edit: Btw the code that I was making was reaching a hundred line of code. And this one is just 35 lines.

1 Like