Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How can we make the script save forever?

Asked by 1 month ago

How do we make a script save for every time for example: Just like in tycoon we get money we upgrade and when we leave the server and join back later. we want the tycoon to be saved. we don't want to restart again we want to have our data saved. But i don't really understand how to do so can someone explain how we can make a save script that saves things when we join a new server.

0
This requires DataStoreService, why not a BoolValue that turns true for every stage you buy? For example, when the player touches a button. The script from that button turns the BoolValue into true so when they left, the server will check the BoolValues and their progress is saved. cherrythetree 68 — 1mo
0
ok thanks i will check it. mastercheeseit 58 — 1mo

3 answers

Log in to vote
0
Answered by 1 month ago

It saves a script to the stringvalue, so it saves script forever.

    local SaveScriptForever = Instance.new("StringValue")
    SaveScriptForever.Name = "SaveScriptForever"
    SaveScriptForever.Value = "a"
    SaveScriptForever.Parent = game.ServerStorage
        scriptpathyouwanttosaveforever.Source = SaveScriptForever.Value
Ad
Log in to vote
0
Answered by
imKirda 1233 Moderation Voter
1 month ago
Edited 1 month ago

You will have to use DataStoreService, it saves the data to the cloud.

Here is a little example of how to save data into the data store, i haven't tested it but what it does is that it takes all values from a specific folder (that you can select) and gets their names and then saves them into a table. Whenever a player joins, you should create a folder for him and whenever he buys a structure, create instance which will have the structure's name and put it into the folder (no matter what instance). Then when he joins back it should return the table with saved structure's strings and all you have to do then is just to check if player has the structure by it's name. Again i haven't tested it but if you will have any errors there you can ask.

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local DataStore = DataStoreService:GetDataStore("YOUR_DATA_STORE_NAME")

local function SaveData(Player)
    if Player then

        local OwnedStructuresFolder = Player:FindFirstChild("OwnedStructuresFolder") -- Finds the structures folder

        if OwnedStructuresFolder then
            local OwnedStructuresToSave = {} -- Table in which we will save the strings

            for _, OwnedStructure in pairs(OwnedStructuresFolder:GetChildren()) do  -- For every single value in the folder do (_ is variable for index but we won't need it)

                table.insert(OwnedStructuresToSave, OwnedStructure.Name) -- Inserts the structure into the table
            end

            local SaveKey = Player.UserId.."-OwnedStructures" -- You must save the data into key in the data store
            pcall(function()    -- Pcalls are often used when saving data, you can read more about them in the Roblox wiki
                DataStore:SetAsync(SaveKey, OwnedStructuresToSave) -- Saves the data into data store
            end)
        end
    end
end

local function LoadData(Player)
    if Player then

        local OwnedStructuresFolder = Player:FindFirstChild("OwnedStructuresFolder")
        local RestoredData = {} -- Table in which restored data will go
        local SaveKey = Player.UserId.."-OwnedStructures"

        local Success = pcall(function()
            RestoredData = DataStore:GetAsync(SaveKey)
        end)

        if Success then -- Success means that there were no errors while getting data
            if RestoredData then -- If it finds any data stored (If player joins for the first time there is not data stored)

                for _, StructureName in pairs(RestoredData) do -- For every single structure stored it will do

                    local RestoredValue = Instance.new("StringValue")   -- Does not matter which instance
                    RestoredValue.Name = StructureName -- It sets the value's name to the saved structure name
                    RestoredValue.Parent = OwnedStructuresFolder
                end
            end
        end
    end
end
Log in to vote
0
Answered by 1 month ago

Hey! By what you are saying, you need to learn about the DataStoreService. Also, you will need to know how to make a leaderboard, and if you dont, you should learn that. Here is an example script, put this in ServerScriptService

-- Services
local Players = game.Players
local DataStoreService = game:GetService("DataStoreService")

-- Data Store
local PlayerData = DataStoreService:GetDataStore("PlayerData")

Players.PlayerAdded:Connect(function(Player)
    -- Leaderboard (Dont change this)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    -- Cash
    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    -- Loading Data
    local Data
    -- Gets Data
    local Success,Error = pcall(function()
        Data = PlayerData:GetAsync(Player.UserId .. "-cash")
    end)
    if Success then
        -- Sets the cash value to the data
        Cash.Value = Data
    else
        warn(Error)
    end
end)
-- Saving Data
Players.PlayerRemoving:Connect(function(Player)
    local Success,Error = pcall(function()
        PlayerData:SetAsync(Player.UserId .. "-cash",Player.leaderstats.Cash.Value)
    end)
    if not Success then
        warn(Error)
    end
end)

To test this, just try and change the players cash in-game, and leave, then join back in. If you need help with this, put the following code in a script with its parent being a part, and touch the part

local Part = script.Parent
local debounce = false

Part.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if Player then
        debounce = true
        local Cash = Player.leaderstats.Cash
        Cash.Value = Cash.Value+10 -- Change this to whatever
        Part.TouchEnded:Wait()
        debounce = false
    end
end)

Hope this helps! Comment if something doesnt work, because i tested it and it should work.

Answer this question