I am making a simple data saving system, and when I use the command to give myself coins, the data store gets saved as zero. I will link the scripts, can someone tell me what is wrong?
(note: if my code is messy, don’t lecture me on that, I want to know what the error with my datastore is, not a review of how organized my code is)
Coin Giving Script
local plrs = game.Players
local developers = {'hellofriend3012'}
plrs.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if table.find(developers,plr.Name) then
local segments = string.split(msg,' ')
if segments[1] == '/dev' then
if segments[2] == 'coins' then
local coins = plr:WaitForChild('leaderstats',1):WaitForChild('Coins',1)
if coins then
if segments[3] then
if tonumber(segments[3]) then
coins.Value += tonumber(segments[3])
else
coins.Value += 50
end
end
end
end
end
end
end)
end)
Saving Script
local coinsStore = game:GetService('DataStoreService'):GetDataStore('coinsStore')
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = plr
local coins = Instance.new('NumberValue')
coins.Name = 'Coins'
coins.Value = 50
coins.Parent = leaderstats
local coinAmount = coinsStore:GetAsync(plr.UserId)
if coinAmount then
print('Coins Found')
print(coinAmount)
coins.Value = coinAmount
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local uId = plr.UserId
local coins = plr:WaitForChild('leaderstats',1):WaitForChild('Coins',1)
if coins then
local value = tonumber(coins.Value)
print(value)
if value then
print('Setting Value')
coinsStore:SetAsync(uId,value)
end
end
end)
It keeps saying it got 50 coins, when it is somewhere around 10,000 coins.
Thank you!
It’s still not registering it. Here is the new script.
local coinsStore = game:GetService('DataStoreService'):GetDataStore('coinsStore')
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = plr
local coins = Instance.new('NumberValue')
coins.Name = 'Coins'
coins.Value = 50
coins.Parent = leaderstats
local function getData()
local success, coinAmount = pcall(function()
return coinsStore:GetAsync(plr.UserId)
end)
if success and coinAmount then
print('Coins Found')
print(coinAmount)
coins.Value = coinAmount
else
wait(6)
getData()
end
end
getData()
end)
game.Players.PlayerRemoving:Connect(function(plr)
local uId = plr.UserId
local coins = plr:WaitForChild('leaderstats',1):WaitForChild('Coins',1)
if coins then
print('Saving data, found coins.')
local function saveData()
local success, errorMessage = pcall(function()
return coinsStore:SetAsync(plr.UserId,coins.Value)
end)
if success then
print('Data saved successfully')
else
print('Error in saving data.')
wait(6)
saveData()
end
end
saveData()
end
end)
It makes it to ‘Saving data, found coins.’ but doesn’t do anything else.
Maybe define the function outside of the player removing event or just dont define one at all. Also, I don’t think you can run a function within the function like you did here:
else
print('Error in saving data.')
wait(6)
saveData()
Try this:
game.Players.PlayerRemoving:Connect(function(plr)
local uId = plr.UserId
local coins = plr:WaitForChild('leaderstats',1):WaitForChild('Coins',1)
if coins then
print('Saving data, found coins.')
local success, errorMessage = pcall(function()
coinsStore:SetAsync(plr.UserId,coins.Value)
end)
if success then
print('Data saved successfully')
else
print('Error in saving data. Error: '..errorMessage)
end
end
end
end)
I found the error. I was using something like this:
local success, coinAmount = pcall(getCoins(userId))
This may seem crazy, but I actually used the new Assistant AI and asked it for help. Apparently, I was not supposed to call the function in the pcall, but the name of the function and then its subsequent parameters without using another pair of parentheses. Like this:
Original Code
local success, coinAmount = pcall(getData(userId))
Fixed Code
local success, coinAmount = pcall(getData, userId))
I’m actually pretty pleased with this Assistant AI. I’ll probably use it in the future. Thanks for the help!