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

I have this data store script, but it doesn't work. Could someone help?

Asked by 3 years ago

Here's the script I have for the data store.

local DataStoreService = game:GetService("DataStoreService")
local MBucksDataStore = DataStoreService:GetDataStore("MBucksDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "playerstats"

    local Cash = Instance.new("IntValue", leaderstats)
    Cash.Name = "MBucks"

    local data
    local userID =  player.UserId

    local success, errormessage = pcall(function()
        data = MBucksDataStore:GetAsync(userID)
    end)    

    if success then
        Cash.Value = data
    end
end)


game.Players.PlayerRemoving:Connect(function(player)
    local userID =  player.UserId

    local data = player.playerstats.MBucks.Value

    local success, errormessage = pcall(function()
        MBucksDataStore:SetAsync(userID, data)
    end)

    if success then
        print("Data succesfully saved.")
    else
        print("There was an error when saving data.")
        warn(errormessage)
    end
end)

It won't even print the error message or warn the error message. There are no errors in the output, and I did change it in the server. Please help.

0
are you in studio Raccoonyz 1092 — 3y
0
Yes. FireSam5163 22 — 3y
0
did you enable api services in studio Raccoonyz 1092 — 3y
0
yes FireSam5163 22 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I had the same problem when i followed a script from a YouTube video. Try using this script instead, it worked for me.

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

local Datastore = DataStoreService:GetDataStore("MyDataStore");

Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player);
    leaderstats.Name  - "leaderstats";

    local cash = instance.new("IntValue", leaderstats);
    cash.Name =  "Cash";

    local data = nil;

    local s, e = pcall(function()
        data = Datastore:GetAsync(player.UserId.."-cash")
    end)

    if(s) then
        print("data successfully loaded")
        data = data or 0 -- if no data has been saved, it replaces it with 0 for default
        cash.Value  = data
    else
        warn("There was a problem loading data:", e);
    end
end);


Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player.leaderstats
    local cash = leaderstats.Cash

    local s, e = pcall(function()
        Datastore:SetAsync(player.UserId.."-cash", cash.Value)
    end);

    if(s) then
        print("data successfully saved");
    else
        warn("There was a problem saving data:", e);
    end
end)
Ad

Answer this question