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

Why isn't Data isn't being stored/loaded?

Asked by 3 years ago

For some reason the data I have isn't being stored (Only tested in studio). Yes I do have allow APIs on. Any and all help would be appreciated.

1st script store in ServerScriptStorage named "Startup"

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local DSS = game:GetService("DataStoreService")
local Datastore = DSS:GetDataStore("Test")

local ReplicatedModules = RS:WaitForChild("Modules")
local InsCreator = require(ReplicatedModules.InstanceCreator)

local function PlayerAdded(plr)
    local leaderstats = InsCreator.create("Folder",{Name = "leaderstats",Parent = plr})
    local pizza = InsCreator.create("IntValue",{Name = "Pizza",Value = 0})
    local cash = InsCreator.create("IntValue",{Name = "Cash",Value = 0})
    local rebirths = InsCreator.create("IntValue",{Name = "Rebirths",Value = 0})

    local key = string.format("%s's data",plr.UserId)

    local success, err = pcall(function()
        local save = Datastore:GetAsync(key)
        if save then
            pizza.Value = save[1]
            cash.Value = save[2]
            rebirths.Value = save[3]
        end
    end)

    if success then
        print(string.format("loaded %s's data",plr.Name))
    elseif err then
        warn(err)
        print(string.format("Failed to load %s data",plr.Name))
    end

    leaderstats.Parent = plr
    pizza.Parent = leaderstats
    cash.Parent = leaderstats
    rebirths.Parent = leaderstats

end

local function PlayerRemoving(plr)
    local key = string.format("%s's data",plr.UserId)
    local ls = plr.leaderstats
    local success,err = pcall(function()
        Datastore:SetAsync(key,{ls.Pizza.Value,ls.Cash.Value,ls.Rebirths.Value})
    end)
    if success then
        print(string.format("saved %s's data",plr.Name))
    elseif err then
        warn(err)
    end
end




Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

2nd script(module script) stored in Replicated Storage > Modules named InstanceCreator

local InstanceCreator = {}

function InstanceCreator.create(class,properties)
    local newItem = {}
    newItem = Instance.new(class)
    for i, v in pairs(properties) do 
        newItem[i] = v
    end
    return newItem
end


return InstanceCreator

1 answer

Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
3 years ago

We found the problem in chat, he was testing in studio, not the game.

Ad

Answer this question