I’m trying to make a datastore script but all of a sudden these “value of type nil cannot be converted to a number” errors are appearing.
Data is being found but it won’t be set onto the players statistics(such as cash) take a look at the script:
local Services = {
ReplicatedStorage = game:GetService("ReplicatedStorage"),
Players = game:GetService("Players"),
UIS = game:GetService("UserInputService"),
RunService = game:GetService("RunService"),
DataService = game:GetService("DataStoreService"),
}
local default_Set = {
Cash = 1500,
}
local CoreData = Services.DataService:GetDataStore("CoreData_010l")
local storeName = "data_"
local function onPlayerJoined(player:Player)
local Cash = Instance.new("NumberValue", player)
Cash.Name = "Cash"
local storeCashes = Instance.new("StringValue",Cash)
storeCashes.Name = storeName
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Reputation = Instance.new("NumberValue",leaderstats)
local Wanted = Instance.new("NumberValue",leaderstats)
Reputation.Name = "Rep."
Wanted.Name = "Wanted"
local storeRep = Instance.new("StringValue",Reputation)
storeRep.Name = storeName
local storeWanted = Instance.new("StringValue",Wanted)
storeWanted.Name = storeName
local playerID = "Player_"..player.UserId
local data = CoreData:GetAsync(playerID)
if data then
print("Data found for this player, setting...")
Cash.Value = data["Cash"]
Reputation.Value = data["Reputation"]
Wanted.Value = data["Wanted"]
else
print("No data found for this player.")
Cash.Value = default_Set.Cash
end
end
local function createSession(player:Player)
local plr_data = {}
for _, v in ipairs(player:GetDescendants()) do
if v:FindFirstChild(storeName) then
plr_data[v.Name] = v.Value
end
return plr_data
end
end
local function onPlayerLeft(player:Player)
local plr_data = createSession(player)
local success, err = pcall(function()
local userID = 'Player_'..player.UserId
CoreData:SetAsync(userID, plr_data)
end)
if not success then
warn("Unable to save session.")
end
end
Services.Players.PlayerAdded:Connect(onPlayerJoined)
Services.Players.PlayerRemoving:Connect(onPlayerLeft)
local function createSession(player: Player)
local plr_data = {}
for _, v in ipairs(player:GetDescendants()) do
if v:IsA(“NumberValue”) or v:IsA(“StringValue”) then
plr_data[v.Name] = v.Value
end
end
return plr_data
end
Ah, i got it, in your createSession function, you are returning after 1 iteration, take your return outside of the for loop
local function createSession(player:Player)
local plr_data = {}
for _, v in ipairs(player:GetDescendants()) do
if v:FindFirstChild(storeName) then
plr_data[v.Name] = v.Value
end
end
return plr_data
end
Replace the block of code with this one that has print commands:
if data then
print("Data found for this player, setting...")
print("Cash.Value =", Cash.Value)
print("Reputation.Value =", Reputation.Value)
print("Wanted.Value =", Wanted.Value)
Cash.Value = data["Cash"]
Reputation.Value = data["Reputation"]
Wanted.Value = data["Wanted"]
else
print("No data found for this player.")
Cash.Value = default_Set.Cash
end
18:39:33.553 Data found for this player, setting... - Server - NewData:38
18:39:33.554 Cash.Value = 0 - Server - NewData:40
18:39:33.554 Reputation.Value = 0 - Server - NewData:41
18:39:33.554 Wanted.Value = 0 - Server - NewData:42
18:39:33.554 value of type nil cannot be converted to a number - Server - NewData:45
18:39:33.555 Script 'ServerScriptService.DS.NewData', Line 45 - function onPlayerJoined
if data then
print("Data found for this player, setting...")
print("Cash.Value =", Cash.Value)
print("Reputation.Value =", Reputation.Value)
print("Wanted.Value =", Wanted.Value)
Cash.Value = data["Cash"]
Reputation.Value = data["Reputation"]
Wanted.Value = data["Wanted"]
else
print("No data found for this player.")
Cash.Value = default_Set.Cash
end
end
Doesn’t seem to be printing it, this is what I added:
local function onPlayerLeft(player:Player)
local plr_data = createSession(player)
local success, err = pcall(function()
local userID = 'Player_'..player.UserId
CoreData:SetAsync(userID, plr_data)
end)
if not success then
warn("Unable to save session.")
end
print(plr_data)
end
I also tried printing it inside of the local pcall function