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

Problem with my data store script ? AlvinBlox script didn't work urgent

Asked by
mist0s 38
4 years ago
Edited 4 years ago

Hey, Sorry for using urgent but I need your help. My game is practisly done but I didn't do one thing: Data store. So I read some articles about and see some videos about. I have to store three IntValue: Win/Money/Medicine.

So, I see the AlvinBlox tutorial and I copy exactly the same script ( I have allowed the data in game settings :D ) and the script didn't save the cash (Studio and game) and I need to know why ?

Here is the script

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

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

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

    local data 
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-cash") or 0
        print("Data load")
    end)
    if success then
        cash.Value = data
    else
        print("Error while loading data")
        warn(errormessage)
    end

end)

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("Error while saving data")
    warn(errormessage)
    end
end)

The print("Data saved") work one in a half. If you have some tips for doing that, thanks a lot ! So really thanks for answer to this topic ! :D

0
Have you tried using BindToClose or using my datastore method located in the forum? greatneil80 2647 — 4y
0
why are you even using his tutorials, theyre garbage lol LoganboyInCO 150 — 4y
0
What is BindToClose exaclty ? mist0s 38 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Use this:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CurrencySaveSystsem")

game.Players.PlayerAdded:connect(function(plr)
         local leader = Instance.new("Folder", plr)
         leader.Name = "leaderstats"
         local Currency = Instance.new("IntValue", leader)
         Currency.Name = "Cash"
         Currency.Value = ds:GetAsync(plr.UserId) or 0
         ds:SetAsync(plr.UserId, Currency.Value)
         Currency.Changed:connect(function()
                  ds:SetAsync(plr.UserId, Currency.Value)
          end)
end)

game.Players.PlayerRemoving:connect(function(plr)
     ds:SetAsync(plr.UserId, plr.leaderstats.Cash.Value);
end)
Ad

Answer this question