I am making a datastore that will save the players points and cash but it either says it can’t save or just nothing. I have tried just doing it for one value and the same problems happen. The player will get points by touching a part and cash by touching another part.
-- DataStore
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerDataStore")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue") --Sets up value for leaderstats
cash.Name = "Cash"
cash.Parent = leaderstats
local points = Instance.new("IntValue") --Sets up value for leaderstats
points.Name = "Points"
points.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
cash.Value = data['Cash']
points.Value = data['Points']
else
-- Data store is working, but no current data for this player
cash.Value = 0
points.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
-- Part script
local part = script.Parent
local canGet = true
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player and canGet then
canGet = false
player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1
wait(60)
canGet = true
end
end
end
part.Touched:Connect(onTouch)
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
cash.Value = data['Cash']
points.Value = data['Points']
else
Big mistake, because you have to put it in a pcall to make sure it works.
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success then
cash.Value = data['Cash']
points.Value = data['Points']
else
Side note, but you should also check if the data is equal to a saved Number so that it doesn’t error as a nil value:
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success and data then
cash.Value = data['Cash']
points.Value = data['Points']
else
They do work in studio? Are you slow lol? Its because the server sometimes closes before the player leaves causing it not to save!. Thats why you should use game:BindToClose(function() end)!
Does this go on line 78 in the original code which is:
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
-- DataStore
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerDataStore")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue") --Sets up value for leaderstats
cash.Name = "Cash"
cash.Parent = leaderstats
local points = Instance.new("IntValue") --Sets up value for leaderstats
points.Name = "Points"
points.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success and data then
cash.Value = data['Cash']
points.Value = data['Points']
else
-- Data store is working, but no current data for this player
cash.Value = 0
points.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
print(create_table(player))
-- DataStore
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerDataStore")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue") --Sets up value for leaderstats
cash.Name = "Cash"
cash.Parent = leaderstats
local points = Instance.new("IntValue") --Sets up value for leaderstats
points.Name = "Points"
points.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success and data then
cash.Value = tonumber(data['Cash'][1])
points.Value = tonumber(data['Points'][1])
else
-- Data store is working, but no current data for this player
cash.Value = 0
points.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
print(player_stats)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
Got this: 19:03:02.285 ServerScriptService.DataStore:42: attempt to index number with number - Server - DataStore:42
19:03:02.285 Stack Begin - Studio
19:03:02.285 Script ‘ServerScriptService.DataStore’, Line 42 - function onPlayerJoin - Studio - DataStore:42
19:03:02.286 Stack End - Studio
-- DataStore
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerDataStore")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue") --Sets up value for leaderstats
cash.Name = "Cash"
cash.Parent = leaderstats
local points = Instance.new("IntValue") --Sets up value for leaderstats
points.Name = "Points"
points.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data
local success, errormessage = pcall(function()
data = playerData:GetAsync(playerUserId)
end)
if success then
cash.Value = data['Cash'][1]
points.Value =data['Points'][1]
else
-- Data store is working, but no current data for this player
cash.Value = 0
points.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
print(player_stats)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)