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

I can't Save Player's IntValue Via DataStore?

Asked by
Borrahh 265 Moderation Voter
3 years ago

Basically, I'm trying to Sava Player's Points (IntValue not Leaderstats) But I get Print: Data Not Saved anyone knows why?

local DATASTORE = game:GetService("DataStoreService"):GetDataStore("DATASTORE")
game.Players.PlayerAdded:Connect(function(player)
    local Points = Instance.new("IntValue")
    Points.Name = "Points"
    Points.Parent = player

    local data
    local dataSaved, notSaved = pcall(function()
        data = DATASTORE:GetAsync(player.UserId.."Points")
    end)

    if dataSaved then
        Points.Value = data
        print("Data Saved")
    else
        print("Data not saved")
        warn(notSaved)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local dataSaved, notSaved = pcall(function()
        DATASTORE:SetAsync(player.UserId.."-Points", player.Points.Value)
    end)

    if dataSaved then
        print("Data Saved")
    else
        print("Data not saved")
    end
end)


    local givePoints = game.ReplicatedStorage.givePoints
    givePoints.OnServerEvent:Connect(function(player)
        player.Points.Value = player.Points.Value + 1
    end)

1 answer

Log in to vote
1
Answered by
Nckripted 580 Moderation Voter
3 years ago

I found your problem. This is what you wrote on line 9:

data = DATASTORE:GetAsync(player.UserId .. "Points")

And this is what you wrote on line 24:

DATASTORE:SetAysnc(player.UserId .. "-Points", player.Points.Value)

In other words, this means that you are saving the data with different keys. To explain it a bit more, let's say I played your game and got 10 points. I leave the game, and the data is saved to 459692218-Points. The next day, I join your game once again. The code looks for a key called 459692218Points. This is not a valid save key, and it returns nil.The solution to this is to make it so line 9 has "-Points" instead of "Points"

Hope this helped!

0
Nope, It didn't work :/ I get Print "Data Saved" but data is not being saved (api is enabled) Borrahh 265 — 3y
0
I might know why. Try this: While playing your game in Studio, go to the Test section. There should be a button that says Current: Client. Click on this, and then change the value. This should fix your problem and I can explain why. Nckripted 580 — 3y
0
My Bad Haha, The values were updating from client side, would you mind if you explain a bit why we had to add like "-Points" on datastore? Borrahh 265 — 3y
0
Woops, I also realised that the Value is stuck to 110, and is not updating... Borrahh 265 — 3y
View all comments (4 more)
0
Woops, I also realised that the Value is stuck to 110, and is not updating... Borrahh 265 — 3y
0
So actually, there are many ways save data. I prefer using a table to save it, and then I get values from that table. So just using stuff like "-Points" is just another way of saving data. Also, could you mark my answer as correct please? Nckripted 580 — 3y
0
Oh sry I didn't see that until I did refresh lol. Nckripted 580 — 3y
0
I would assume this is not to do with the save system, however make sure to connect your increment function to a button to see if that will change the value. I assume it will. Nckripted 580 — 3y
Ad

Answer this question