Data Saving like Billionaire Simulator game

Hello, i am new member of DevForum.

Do you know guys how to make a real time tycoon running like “Billionaire Simulator” game, made by DoubleJumpGames.

This team is using httpService? or what? please give me an example of script thank you. :slight_smile:

1 Like

I’m not too familiar with Billionaire Simulator. I checked the game out to see what you might be talking about.

I am assuming you are referring to the reward players get when they rejoin the game based on how long in real life time it’s been since they last played. To accomplish this, save the global Unix timestamp using os.time() when the player leaves the game. This timestamp should be saved with the rest of player data. When they join the game, upon loading their data, compare the current global timestamp to the loaded one, and provide rewards based on the elapsed time.

I wrote an example script, though in practice I’d write this differently. In real use cases, make sure to save the timestamp with the rest of the player’s data so as not to waste DataStore requests unnecessarily.

local services = {
    data_store = game:GetService("DataStoreService"),
    players = game:GetService("Players"),
    run = game:GetService("RunService"),
}

last_played_timestamp_data_store = services.data_store:GetDataStore("last_played_timestamp")

local successfully_loaded_players = {}

local function is_player_guest(player)
    if services.run:IsStudio() == true then
        return false
    end

    return player.UserId < 1
end

local function get_last_played_timestamp(player)
    local function load_saved_timestamp()
        return last_played_timestamp_data_store:GetAsync(player.UserId)
    end

    local is_load_successful, last_played_timestamp = pcall(load_saved_timestamp)

    if is_load_successful == false then
        return is_load_successful
    end

    return is_load_successful, last_played_timestamp
end

local function on_player_added(player)
    if is_player_guest(player) == true then
        return
    end

    local is_load_successful, last_played_timestamp = get_last_played_timestamp()

    if is_load_successful == false then
        return nil
    end

    local current_timestamp = os.time()

    if last_played_timestamp == nil then
        last_played_timestamp = current_timestamp
    end

    local elapsed_time = current_timestamp - last_played_timestamp
    -- Award players with something based on the elapsed time!

    successfully_loaded_players[player] = true
end

local function save_last_played_timestamp(player)
    local function set_timestamp_data()
        last_played_timestamp_data_store:SetAsync(player.UserId, os.time())
    end

    pcall(set_timestamp_data)
end

local function on_player_removing(player)
    if is_player_guest(player) == true then
        return nil
    end

    if successfully_loaded_players[player] == nil then
        return nil
    end

    save_last_played_timestamp(player)

    successfully_loaded_players[player] = nil
end

local function save_all_player_timestamps()
    local players = services.players:GetPlayers()

    for index = 1, #players do
        local player = players[index]

        on_player_removing(player)
    end
end

services.players.PlayerAdded:Connect(on_player_added)
services.players.PlayerRemoving:Connect(on_player_removing)
game:BindToClose(save_all_player_timestamps)
5 Likes

Thank you sir, let me review your script.

Let’s say the player1 have 2k Money in the game and when the player1 leave the game but the tycoon is still running after 2 hours the player1 joined in the game again and now the player1 have 23k money in the game. Can you make a place with leaderStats? you just saved the time, sorry i’m not too good at scripting.

If you’re looking for someone to code your game for you, the Public Collaboration section may be a better place to look. :slight_smile:

If you have any more specific questions about how to apply the example, I’d be happy to help.

3 Likes

No, haha i don’t need a helper to my game, i want to learn by myself. I’ll just wait to the other developers to give me an another example. btw sir, thank you for your kindness. :slight_smile:

1 Like

To create leaderstats, you need to use Instance.new() to create an object with the name "leaderstats", and then you can place ValueBase instances inside it, like so:

local leaderstats = Instance.new("Folder")

local money = Instance.new("NumberValue")
money.Name = "Money"
money.Parent = leaderstats

leaderstats.Parent = player

You can scale the money you’d given based on the elapsed time like so:

local money_per_second = 1

local money_to_give = elapsed_time * money_per_second

Then, you’d proceed to add the value of money_to_give to the player’s current money value.

Yeah, i know how to create leaderStats i only need the real time data saving, and while players is not in my game the money will increase per minute and save it. I thought i need to create a website to handle the data of player using httpService

Right, that’s what my first example tries to accomplish. You’re not saving the money repeatedly while the player is not in the game, instead, you get the elapsed time since the player last left the game, and you calculate how much of whatever to add as a function of that elapsed time.

I’ll try to understand it, I need to learn more about scripting.

Roblox Scripting Book is not enough for me.

That may be the case. If you’re able to point out any particular parts that confuse you in my example code, I’d probably be able to explain them better to you. Otherwise, I wish you luck continuing to learn to script!

I’m currently developing a game called Project: Last Man Standing It’s functional

So, i can use the os.time() and DataStoreService as Daily login too right? or no?

Yes, the concepts are the same.

1 Like

Btw sir, how did you saw this topic? Sorry i’m new.

https://devforum.roblox.com/c/development-support/scripting-support

1 Like

I don’t mean to be discourteous when I say this, but I believe that your ambition overextends your skill level. If you are new to scripting, you should focus on understanding how things work before you jump into advanced-level items like calculating how much offline in-game revenue players should earn. This includes exploring the usage of HttpService and DataStores on both an API and practical level, while seeing which one suits your needs.

Above all, you should also score the basics down to help you further utilise such services to your advantage.

There are many articles on the Developer Hub that provide insight on various topics: this includes usage examples, code samples, tutorials and explanations. Take a look at some of them to help you in your journey to learning code. Here is some information on DataStores. There are also various tutorials and developer-written articles on our Medium page, the DevForum and elsewhere.

Welcome the DevForum, by the way. :tada:

8 Likes

I’ve already know about dataStoreService and httpService.

I’m currently learning about os.time()

Right. I most likely misread some of your posts or did not know that you already knew how to work the aforementioned two.

On that note though: no, HttpService is not involved and there is virtually no reason for it to be. The solution prior posted should be what you aim for.

The first thing you need is to check the time difference between player sessions. os.time() returns the number of seconds since January 1, 1970 (UNIX Epoch), so this essentially centralises your time. Once you have this, you can use something like os.difftime() to see how much time has passed between the last server a player joined and their current one. Finally, use this value to apply as a multiplier or divisor for any stats you may possess or want to scale against time, then add that to the player’s data.

2 Likes