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

How would I use os.time?[Still need help.]

Asked by 9 years ago

I read the page for os time and I wonder if I could make a bonus like give the player points every time they play every day.

I think it should work like this:

game.Players.PlayerAdded:connect(function(player)
    if player:WaitForDataReady() and player:LoadNumber("TimeSinceLastOn") < os.time(day) then
        player.leaderstats.Points = player.leaderstats.Points +10
    end
end)

game.PlayerRemoving:connect(function(player)
    player:SaveNumber("TimeSinceLastOn", os.time(day))
end)

game.OnClose:connect(function()
    wait(30)
end)


--Am I close? How would I do this for hours, years, minutes, or seconds even?

So would this work as a ban script?

game.Players.PlayerAdded:connect(function(plyr)
    local banned = false
    if plyr:WaitForDataReady() and plyr:LoadNumber("BanTimeLeft") >= 0 then
    print(plyr.Name..", don't curse again.")
    elseif plyr:LoadBoolean("IsBanned") and BanIfCurse then
        Instance.new("Message", plyr).Text = "You're banned for cursing. You may play again after: "..plyr:LoadNumber("BanTimeLeft")
    end
    plyr.CharacterAdded:connect(function(char)
        plyr.Chatted:connect(function(chat, chatter)
            if chat == "[Content Deleted]" and BanIfCurse then
                plyr:SaveBoolean("IsBanned", true)
                plyr:SaveBoolean("BanTimeLeft", BanTime)
                plyr:Kick()
            else

            end
        end)
    end)
end)

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

day isn't defined, so using that wouldn't make sense.


os.time() returns number of seconds; a day is 60 * 60 * 24 seconds.


Besides that, your script is missing something. You will punish players for coming back more often than every 24 hours, since every time they leave, the time is reset.

Instead, reset the time when they get their reward.

function join(player)
    if player:WaitForDataReady() then
        local nextReward = player:LoadNumber("NextReward")
        local newPlayer = nextReward == 0
        -- 
        local now = os.clock()
        -- if now is before next reward time (or they are a new player)
        if now <= nextReward or newPlayer then
            -- <Give reward here>
            local tomorrow = now + 60 * 60 * 24 -- now plus a day
            player:SaveNumber("NextReward", tomorrow )
        end
    end
end

It's very very similar for identifying someone for, say, a day:

local banTime = 60 * 60 * 24 -- one day

function ban(player)
    local now = os.clock()
    -- They are banned until now plus one day
    player:SaveNumber("BannedUntil", now + banTime)
    player:Kick()
end

function join(player)
    player:WaitForDataReady()
    local now = os.clock()
    if now < player:LoadNumber("BannedUntil") then
        -- now is before ban ends?
        player:Kick()
    end
end
0
So how could I turn this into a temporary ban script? EzraNehemiah_TF2 3552 — 9y
0
Pretty much the same. Store a "bannedUntil" and check is the current time is before that time. (If not set, :LoadNumber returns 0, which is 1970, which now is not before, so you don't need any special case for new players) BlueTaslem 18071 — 9y
0
Blue, I edited my question and I added an unfinished script, I don't know how to finish it. Most of it isn't right, I know. I just need help with the ban part. EzraNehemiah_TF2 3552 — 9y
0
@Blue would this still work if the player entered a different server? BSIncorporated 640 — 9y
0
Yes, excepting caches -- they might be able to join one server, quick, join another, be banned, then join that first server. But that is not a deficiency of the script, it's just that BlueTaslem 18071 — 9y
Ad

Answer this question