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