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

DataStore dont save my stats. Help pls ?

Asked by 4 years ago
Edited 4 years ago

I create this script , and put it in the "ServerScript Service But when i change the Value Cash1, it wont save it. I call the Cash from a localscript in Screen Gui When i start the Server again the Cash.Value is 0 ....

so i call the file


local player = game.Players.LocalPlayer
local levelpoint = player.leaderstats.Cash1

levelpoint.Value = levelpoint.Value +5


this is the DAtastore script

local DataStoreServer = game:GetService("DataStoreService") local myDataStore = DataStoreServer:GetDataStore("myDataStore")

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

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

local data
local succes, errormessage = pcall(function()
    data = myDataStore:GetAsync(player.UserId.."Cash1")
end)

if succes then
    Cash1.Value = data
else
        print("there was an error ")
        warn(errormessage)
end

end) game.Players.PlayerRemoving:Connect(function(player) local succes , errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."Cash1",player.leaderstats.Cash1.Value)

    end)

if succes then
    print("player data succesfully saved")
    warn(errormessage)
end

end)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Script in ServerScriptService!

local dataStoreService = game:GetService('DataStoreService')
local DS = dataStoreService:GetDataStore('Cash')

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = plr

    local currency = Instance.new('IntValue')
    currency.Name = 'Cash'
    currency.Value = DS:GetAsync(plr.UserId) or 0
    currency.Parent = leaderstats

    currency.Changed:Connect(function()
        DS:SetAsync(plr.UserId, currency.Value)
    end)
end)

If you publish game, click 'Game Settings', in 'Options' enable Studio Access to API Services (click ON)! Can autosave leaderstats :) Try in console log

game.Players."Your Username".leaderstats.Cash.Value = 5
0
I've already done it .... catkidmiau -1 — 4y
0
Good! MrRobert10016666 0 — 4y
0
did not help catkidmiau -1 — 4y
0
need a publish game! in Roblox Studio click 'Game Settings', in 'Options' enable Studio Access to API Services (click ON)! um... MrRobert10016666 0 — 4y
0
ok, and how i can change the cash from scrip ? call the leaderstat from localscript ? catkidmiau -1 — 4y
Ad

Answer this question