The problem is that player.userId contains a read only integer while data store requires strings for its arguments. Try replacing replacing it with tostring(plr.userId)
So like this
local datastore = game:GetService("DataStoreService")
local CashData = datastore:GetDataStore("CashSavingSystem")
local InBucketData = datastore:GetDataStore("InBucketSavingSystem")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local cd = Instance.new("IntValue", folder)
cd.Name = "Cash"
cd.Value = CashData:GetAsync(tostring(plr.userId)) or 0
local ib = Instance.new("IntValue", folder)
ib.Name = "InBucket"
ib.Value = InBucketData:GetAsync(tostring(plr.userId)) or 0
CashData:SetAsync(tostring(plr.userId), cd.Value)
InBucketData:SetAsync(tostring(plr.userId), ib.Value)
cd.Changed:connect(function()
CashData:SetAsync(tostring(plr.userId), cd.Value)
end)
ib.Changed:connect(function()
InBucketData:SetAsync(tostring(plr.userId), ib.Value)
end)
end)
A little side note to, you shouldn't save every time the players money changes but instead save when the player leaves as its much more efficient. So something like this:
local datastore = game:GetService("DataStoreService")
local CashData = datastore:GetDataStore("CashSavingSystem")
local InBucketData = datastore:GetDataStore("InBucketSavingSystem")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local cd = Instance.new("IntValue", folder)
cd.Name = "Cash"
cd.Value = CashData:GetAsync(tostring(plr.userId)) or 0
local ib = Instance.new("IntValue", folder)
ib.Name = "InBucket"
ib.Value = InBucketData:GetAsync(tostring(plr.userId)) or 0
CashData:SetAsync(tostring(plr.userId), cd.Value)
InBucketData:SetAsync(tostring(plr.userId), ib.Value)
game.Players.PlayerRemoving:Connect(function()
CashData:SetAsync(tostring(plr.userId), cd.Value)
InBucketData:SetAsync(tostring(plr.userId), ib.Value)
end)
end)
3 comments
Is API on?
Yes, API is on.
Don't save to the DataStore every time 'cd' or 'ib' changes. You will exceed data store limits this way. Instead, save at important points like when the player leaves: https://developer.roblox.com/articles/Datastore-Errors