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

Datastore not saving or loading?

Asked by 4 years ago

My datastore isnt working. It's not giving me any errors or prints in the output. What is going on?

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

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
    Cash.Value = 0

    local speed = Instance.new("IntValue")
    speed.Name = "TopSpeed"
    speed.Parent = leaderstats
    speed.Value = 16


    local playerUserId = "Player_"..player.UserId

    local data 
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
    end)

    if success then
        --Set our data equal to the current Cash
        if data then 
            Cash.Value = data.Cash
            speed.Value = data.speed
        end
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local data = {
        Cash = player.leaderstats.Cash.Value,
        speed = player.leaderstats.TopSpeed.Value;
    }
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

    if success then
        print("Data saved successfully")
    else 
        print("There was an error.")
        warn(errormessage)
    end


end)

1 answer

Log in to vote
1
Answered by 4 years ago

I found an error in your script.

40      local data = {
41          Cash = player.leaderstats.Cash.Value,
42          speed = player.leaderstats.TopSpeed.Value;
43      }

That part should be

40      local data = {
41          Cash = player.leaderstats.Cash.Value;
42          speed = player.leaderstats.TopSpeed.Value
43      }

And also this part too

27      if success then
28          --Set our data equal to the current Cash
29          if data then
30              Cash.Value = data.Cash
31              speed.Value = data.speed
32          end
33      end

This should be

27      if success then
30          Cash.Value = data.Cash
31          speed.Value = data.speed
32    end

I hope it works!

Ad

Answer this question