Hello!
I've been working on a data store script, but when I changed my cash value and test played again, it didn't work. I've checked the script for errors, and there are none, it also didnt print anything in the output, I have API Services on, and still dosent work, even published it.
Here's script:
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder", plr) leaderstats.Name = "leaderstats" local cash = Instance.new("IntValue", leaderstats) cash.Name = "Points" local playerUserId = "Player_"..plr.UserId print(playerUserId) --Loads data local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(playerUserId) end) if success then --Set data equal to current cash cash.Value = data end end) game.Players.PlayerRemoving:Connect(function(plr) local playerUserId = "Player_"..plr.UserId print(playerUserId) local data = plr.leaderstats.Points.Value local success, errormessage = pcall(function() myDataStore:SetAsync(playerUserId, data) end) if success then print("Data saved") else print("Save error") warn(errormessage) end end)
Even if it does load the data succesfully, it doesn't mean the data is a integer. It could still be nil. Try checking if it's not nil first.
if success and data ~= nil then --Set data equal to current cash cash.Value = data end
Before you test this, make sure to start with a new datastore to prevent loading in previous data.
local myDataStore = DataStoreService:GetDataStore("myDataStore2")
Hope this helps :D
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore2") game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder", plr) leaderstats.Name = "leaderstats" local cash = Instance.new("IntValue", leaderstats) cash.Name = "Points" local playerUserId = "Player_"..plr.UserId print(playerUserId) --Loads data local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(playerUserId) end) if success and data ~= nil then --Set data equal to current cash cash.Value = data end end) game.Players.PlayerRemoving:Connect(function(plr) local playerUserId = "Player_"..plr.UserId print(playerUserId) local data = plr.leaderstats.Points.Value local success, errormessage = pcall(function() myDataStore:SetAsync(playerUserId, plr.leaderstats.Points.Value) end) if success then print("Data saved") else print("Save error") warn(errormessage) end end)