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

How to give a player coins every day? + Script not working

Asked by 9 years ago

I'm wondering, how to give a player in-game points everyday? I've checked the wiki, but people could leave and rejoin the game alot to get lots of points, but how do you restrict it to only once a day? I know you can't use

wait(86400)

because the player will have to be in the game for 24 hours. So how?

local DataStore = game:GetService("DataStoreService"):GetDataStore("Points")

game.Players.PlayerAdded:connect(function(player)
    local key = "user_".. player.userId
    DataStore:UpdateAsync(key, function(oldValue)
        local newValue = oldValue or 0
        newValue = newValue + 50
    end)
end)

and I mixed it with this :

local points = game:GetService("DataStoreService"):GetDataStore("Points")
local text = script.Parent.TextLabel
while wait() do
    text = "Coins : ".. points
end

one of the scripts is not working because when I join it doesn't show that I have 50 credits.

2 answers

Log in to vote
0
Answered by 9 years ago

To do what you have requested you must keey track of the time that the daily points were last added.

Example of saving time and points in seperate values (this of course uses more requests)

local DataStore = game:GetService("DataStoreService"):GetDataStore("Points")
game.Players.PlayerAdded:connect(function (player)
    local days = math.floor((os.time() - DataStore:GetAsync('Time_' .. player.userId)) / (24 * 60 ^ 2)) -- Get how many days have passed
    if days > 0
        DataStore:UpdateAsync(key, function (oldValue)
            return oldValue + 50 -- Return new value with Points added
        end)
        DataStore:SetAsync('Time_' .. player.userId, os.time())
    end

end)

Example of saving both time and points in one value (uses less requests)

local DataStore = game:GetService("DataStoreService"):GetDataStore("Points")
game.Players.PlayerAdded:connect(function (player)
    DataStore:UpdateAsync('user_' .. player.userId, function (data)
        local days = math.floor((os.time() - data.Time) /  (24 * 60 ^ 2)) -- Get how many days have passed
        if days > 0 then
            return { -- Return the new data
                Time = os.time() -- Set the new current Time (the time daily bonus was last added)
                Points = (data.Points or 0) + 50 -- Add new Points.
            }
        end
    end
end)

Using this method you will have to slightly change how you get and set values Example:

local Points = DataStore:GetAsync('user_' .. player.userId).Points -- Get points

DataStore:UpdateAsync('user_' .. player.userId, function (data) -- Set points
    return {
        Time = data.Time -- Keep old Time
        Points = data.Points + 50 -- Add new points, or just set new points
    }
end)

0
Which one do you suggest I use? I need an answer because I'm trying to get this game finished near Halloween because of its theme theawesome624 100 — 9y
0
I, personaly, would use the second method as it uses less DataStore requests, you would just need to chage how you get and set points. Epidemiology 116 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

What You Need To Do

So, the idea is pretty basic right? Daily rewards. Essentially what you have to do is save the user's current time with DataStores when they leave the game, then when they join the game load up that time and compare it to the current time. If the difference is greater or equal to 24 hours then reward them!

****os.time or tick?

As you may know, there are two ways to get the current time for the user. One way, is to use the tick function. This function returns the user's local time. Another way is to use os.time, which is a shared timezone for all OS's.

So, what should you use? Due to tick returning local times, then a user can just change the time on their console and abuse the daily rewards. You definitely don't want any of that happening. So, you need to be using os.time, since it's shared for all consoles.

****Code

--Make your DataStore
local ds = game:GetService('DataStoreService'):GetDataStore('Daily')

--When a player joins
game.Players.PlayerAdded:connect(function(plr)
    --Check if they have any data saved
    local data = ds:GetAsync(plr.userId..'_data')
    if data ~= nil then
        --Get the current time
        local current = os.time()
        --Check if difference is at least a day
        if current - data >= 60*60*24 then

            --[[Reward the player]]

        end
    end
end

--When a player leaves
game.Players.PlayerRemoving:connect(function(plr)
    --Save current os.time to a personal key
    local key = plr.userId..'_data'
    local current = os.time()
    ds:SetAsync(key,current)
end)

Hope this helps!

Don't forget to accept this answer, it gives us both rep!

0
Best not to tick() since it is dependant on the local machine's time it may (also the users' local time is irrelevant as DataStores can only be accessed from the Server) causing the time between daily points added to be shorter or longer than 24 hours. Epidemiology 116 — 9y
0
I'll try both answers but what about the other script? One of the two scripts aren't working. theawesome624 100 — 9y

Answer this question