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

Why Is my script for data Store is not working I need someone to explain?

Asked by 4 years ago
Edited 4 years ago

Here is my script.

--Locals
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

--Stats adder
game.Players.PlayerAdded:Connect(function(player)
    local leaderStats = Instance.new("Folder")
    leaderStats.Name = "leaderstats"
    leaderStats.Parent = player

    --Cash stats
    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderStats
    -- Load Data

    local CashData


    local Success, errormessage pcall(function()
        CashData = MyDataStore:GetAsync(player.UserId.."-Cash")
    end)
    if Success then
        Cash.Value = CashData
        print("Data Loaded")
    else
        print("Data was NOT loaded :(")
        warn(errormessage)
    end
end)

--Saveing Stats
game.Players.PlayerRemoving:Connect(function(player)
    local Success, errormessage pcall(function() 
        MyDataStore:SetAsync(player.UserId.."-Cash",player.leaderstats.Cash.Value)

    end)
    if Success then
        print("Data Saved")
    else
        print("Data was not saved :(")
        warn(errormessage)
    end
end)
0
What is not working exactly? rabbi99 714 — 4y

2 answers

Log in to vote
0
Answered by
MediaHQ 53
4 years ago

I have a solution if API services are disabled, turn that on. If that doesn't work then I have my own leaderboard that saves and is much smaller. Feel free to use it

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local cash = Instance.new("IntValue", folder)
    cash.Name = "Money"

    cash.Value = ds1:GetAsync(plr.UserId) or 500 --You can replace 500 with how much you want the starting money to be at
    ds1:SetAsync(plr.UserId, cash.Value)

    cash.Changed:connect(function()
        ds1:SetAsync(plr.UserId, cash.Value)
    end)
end)
Ad
Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
4 years ago

I see you are doing this:

local Success, errormessage pcall(function()

But where is =?

Try this:

local Success, errormessage = pcall(function()

There also could be some stuff about it in the output, this is where you should look for issues.

0
it just printing the errormessage in the out put Xx0966xX 41 — 4y
0
what does the error message say? imKirda 4491 — 4y

Answer this question