i know this is such bad practice and janky but at heart why doesnt it work?
hi, i am trying to write a script that would just remember a value ( dollars ) even after the player rejoins, and have been testing it by playing , then going into the code and changing the value of the intvalue created.
No exceptions are raised, but it is not working as intended
i know this is such bad practice and janky but at heart why doesnt it work?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local database = DataStoreService:GetDataStore("data")
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local dollars = Instance.new("IntValue")
dollars.Name = "dollars"
dollars.Parent = leaderstats
print(database:GetAsync(player.UserId))
dollars.Value = database:GetAsync(player.UserId)
end
local function onPlayerRemoving(player)
local leaderstats = player:WaitForChild("leaderstats")
local dollars = leaderstats:WaitForChild("dollars")
print(player , player.UserId)
print(dollars.Value)
database:SetAsync(player.UserId,dollars.Value)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
i have just wrapped the retrieval in a pcall function like shown
no errors in console , success = true but data is nill, so its not setting “dollars” to the data
i have not used pcall function like this i have always seen it as:
local success , errorMsg = pcall(function()
print("bla bla")
end)
does it work differently now? what are the paremeters
The code you provided works for me on a newly created baseplate – so it seems that there isn’t any actual errors with the datastore code itself.
I would guess to try and see if the data saves & loads on a live server in your game; if it still doesn’t save/load there, I would assume there could be another script interfering with the saving process.
The code below uses pcall to output warnings if there are any.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local database = DataStoreService:GetDataStore("data")
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local dollars = Instance.new("IntValue")
dollars.Name = "dollars"
dollars.Parent = leaderstats
local success,plrdollars = pcall(function() -- Instead of calling GetAsync twice, we'll store it in a variable.
return database:GetAsync(player.UserId)
end)
if not success then
warn("Data Get Failed",plrdollars) -- plrdollars becomes the error message/response.
return
end
print("Got dollars", plrdollars)
dollars.Value = plrdollars
end
local function onPlayerRemoving(player)
local leaderstats = player:WaitForChild("leaderstats")
local dollars = leaderstats:WaitForChild("dollars")
print(player , player.UserId)
print(dollars.Value)
local success,response = pcall(function()
database:SetAsync(player.UserId,dollars.Value)
end)
if not success then
warn("Data Save Failed",response)
else
print("Saved!")
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
i have enabled the api thing on a new baseplate and have tried to run it. after loading in, i changed the int value of “dollars” of my character to a random number manually ( e.g going into the object explorer) and then leaving. when i rejoined i did not have the same amount of dollars as last session, no errors were raised. there shouldnt be another script interfering as this is a new base plate. here is the output window
it did have this error tho … but idk if its related or helpful. this might have been an error from getting the value/ setting the value, but it did not appear every time i tested it
Oh that warning is just from one of Roblox’s internal CoreScripts
I think I know what the issue is now though – Are you changing the value on the explorer while in Client view? If so, the changes aren’t replicated to the server which would explain why it’s not saving your data changes.
Press this to toggle between client and server view.
Once you are in server view, you can edit the dollars value there and hopefully it should save
Sometimes datastore saving does not actually work solely on studio (I have experienced this myself a couple times) so try publishing the game and testing it on the actual website rather than studio.
i do the data ~= nil bit so that loading data once retrieved can be caught if nil. if it is nil, it will catch it and just make the value 0 (which is what usually happens when a player joins for the first time unless you want them to start with x amount, if so just write an elseif to check if nil, then set amount you want.) it doesnt work much differently, it just has the inbuilt check of if data is not nil