Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Using tick() to do money per minute?

Asked by 3 years ago

so I'm trying to make a system that gives you 1 money per minute, but I wanted it to work and give you the amount equal to however long they were gone divided by 60, and then rounded down, thus giving me how many minutes they were gone, and I would then give them 1 money for every minute they were gone.

I have the more part of the script written, but the issue I have is that it either doesn't work without having any errors, or it doesn't wait the 60 seconds to give it to me, so I'm stuck getting either nothing, or getting about 10 money per second when I only want 1 money per minute.

this is my code so far (I'm new to scripting so please don't be to harsh on my bad code) :

local players = game:GetService("Players")
local playTable = {}
local ticktable = {}

game.Players.PlayerAdded:Connect(function(player)
    table.insert(playTable, #playTable+1, player.Name)
    table.insert(ticktable, #ticktable+1, tick())
end)

while true do
    wait()
    for table1 = 1, #ticktable, 1 do
        if tick() - ticktable[table1] == 60 then
            local payperson = playTable[table1]
            ticktable[table1] = tick()
            print("paid ".. payperson)
            players:WaitForChild(payperson).leaderstats.Money.Value = players:WaitForChild(payperson).leaderstats.Money.Value + 1
        end
    end
end

I've put it all inside a script in ServerScriptService. please help me, all I really need to know is how to fix the If statment actually stops the for loop and runs the code inside. Thanks :)

2 answers

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago
Edited 3 years ago

Try this:

local players = game:GetService("Players")
local playTable = {}
local ticktable = {}

game.Players.PlayerAdded:Connect(function(player)
    table.insert(playTable, #playTable+1, player.Name)
    table.insert(ticktable, #ticktable+1, tick())
--[[    something for my own debugging, you don't need this bit!
    local leaders = Instance.new("NumberValue",player)
    leaders.Name = "leaderstats"

    local Money = Instance.new("NumberValue",leaders)
    Money.Name = "Money"
]]
end)

function math_round(num)
    return math.floor(num+0.5)
end


while true do
    wait()
    for table1 = 1, #ticktable, 1 do 
        if math_round(tick() - ticktable[table1]) == 60 then 
            local payperson = playTable[table1]
            ticktable[table1] = tick()
            print("paid ".. payperson)
            players:WaitForChild(payperson).leaderstats.Money.Value = players:WaitForChild(payperson).leaderstats.Money.Value + 1
        end
    end
end

Just updated this answer with a working script. I must have had a brain fart in thinking tick() was running at 60fps lol my bad!.

the changes i made simply rounds off the tick() as it returns the time in seconds and the mili secs eg:

0.12523526
1.25642321
etc....

so the above if statement will never fire as its looking for exact numbers!.

it Should be working now but if not let me know! :)

0
for some reason it still doesn't give me the money, or an error in the output, and I directly copied the code you sent into the script. gwenniekins 59 — 3y
0
Just updated my answe with a working script. it just simply rounds off the tick() function. Hope this helps! TGazza 1336 — 3y
0
you had a simple typo, it needed to be >= not == when checking what the time was, but yes it does work now tysm gwenniekins 59 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

It still does nothing in output and doesn't give me money, and I directly copied the code you sent. Is it maybe supposed to have something you forgot to add, or do I need to do something to activate it?

Answer this question