Ad
Log in to vote
0

Help saving data for endings!,How i do this?

Asked by 16 days ago

I need to save the endings or events that the player did and put a block if that player already completed that or not, something like that,but I can't think of any script to help me with that

0
So you need to save position where player put a block to not put it there again? BeautifulAuraLover 371 — 15d

1 answer

Log in to vote
0
Answered by 15 days ago

You need to use a DataStore to do this. It's more efficient to use a bool instead of a block though.

local DataStoreService = game:GetService("DataStoreService")
local EndingStore = DataStoreService:GetDataStore()
--This function is only for the first time joining--
local SetEndings = function(folder)
    local values = {
        ExampleEnding = {
            Name = "ExampleEnding",
            Value = false,
        }
    }
    for _,ending in pairs(values) do
        local value = Instance.new("BoolValue",Endings)
        value.Name = ending.Name
        value.Value = ending.Value
    end
end

game.Players.PlayerAdded:Connect(function(player)
    local key = player.UserId .. "_Endings"
    --key is basically a password for the save--
    local Endings = Instance.new("Folder",player)
    Endings.Name = "Endings"
    local Retrieve
    --Use a pcall because thats just a normal thing in data stores--
    local success,errormessage = pcall(function()
        Retrieve = EndingStore:GetAsync(key)
    end)
    if success then
        if Retrieve ~= nil then
            --It checks if the data exists because it wont the first time you join the experience--
            for _,ending in pairs(Retrieve) do
                local value = Instance.new("BoolValue",Endings)
                value.Name = ending.Name
                value.Value = ending.Value
            end
        else
            SetEndings(Endings)
            --Calls the function made previously--
        end
    else
        warn(errormessage)
    end
end)

--now for the saving process--
game.Players.PlayerRemoving:Connect(function(player)
    local key = player.UserId .. "_Endings"
    --same key as before--
    local Endings = player:WaitForChild("Endings")
    --the folder already exists--
    local List = {}
    for _,ending in pairs(Endings) do
        local item = {}
        item.Name = ending.Name
        item.Value = ending.Value
        table.insert(List,item)
        --it creates a table of values containing the endings the player has achieved before leaving--
    end
    local Success,errormessage = pcall(function()
        EndingStore:SetAsync(key,List)
    end)
    if Success then
        print("Successfully saved endings")
    else
        warn(errormessage)
    end
end)

This is just an example. I haven't tested it, but its basically how to do it. If you want cubes just set a cube if the ending value is true.

Ad

Answer this question