What do you want to achieve? Keep it simple and clear!
A working data store that saves Cash.
What is the issue? Include screenshots / videos if possible!
The code has no errors yet it is not working properly.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried looking over and over the API reference and couldn’t find anything wrong. I’m not sure if it is a spelling error but I just can’t find it
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 playerUserId = "Player_"..player.UserId
--Load data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Cash.Value
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error.")
warn(errormessage)
end
end)
If anyone can help me find out what’s wrong, it would be appreciated.
This has started to work thank you for helping me! I created a new game to override to reset the data store key and turned on ‘access to API services’. Thank you.