Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a script that would execute once everyday for a player?[Like miners haven Daily Gift]

This is not a request! Tell me how to start.

Thanks for helping me!

2 answers

There are two possible things you might be wanting to do:

  1. You want to give the player something every day, regardless of if they login
  2. You want to give the player something whenever they log in, but at most once per day

In both methods you need to log when you awarded the player something last. You will need to store this value in a data store, where the key is the user's id (never user their name, which can change), and where the value is the login date (you should use os.time over tick() when storing time (the link explains why and also has some other useful functions).

You cannot run a script for every player in existence every day (if for no other reason than you don't have enough data store requests for the millions of possible players that might enter your game; see the limits in the Data store article). The better solution is to have a script that runs when a player enters your game. When that happens, calculate the time since they last logged in. You can do a few different things then:

  • You can reward a resource based on how long they have been away (method 1)
  • You can reward a resource and set their "lastAwarded" value to the current time (method 2)

You might choose to set the time to the current day, excluding hours/minutes/etc, so that when someone logs in at slightly different times each day (ex 3pm one day, 4pm the next, then 2pm after that), they'll still get the login, even though it hasn't been 24 hours since the last login.

If they have logged in too early, you might periodically check to see if they can be awarded for logging in yet (until they leave or until you can award them something), or you might just ignore this case.

You want to look into time in roblox, and storing userdata. If you write to a userfile the time and date of the last time they received the award, you can later check to see how much time has passed, and if enough has, allow them to receive the award again.

A starting place to see if their eligible is

local currentTime = tick() --current time in Unix
timeOfLastAwardClaim --The last time the gift was claimed in unix time, gotten from server storage

if timeOfLastAwardClaim + 86400 > currentTime then --86400 is one day in unix time
     --Give award!

     timeOfLastAwardClaim = currentTime
    --Store timeOfLastAwardClaim back into server storage
end

Articles on unix time: https://en.wikipedia.org/wiki/Unix_time

Storing userdata: http://wiki.roblox.com/index.php?title=Saving_Player_Data

Good luck!

2 comments

0
0

Ah, just read through the full article. Nice job catching that :)

This discussion is preserved as it was. New answers, comments, and votes are closed.