So i was trying to make an actual datastore that will work, im not really into datastores but i keep getting error with the player.UserID doesnt exist or is a nil. Basically this
UserID is not a valid member of Player “Players.misulinkaobecna1”
local MyDataStore = game:GetService("DataStoreService")
local playerData = MyDataStore:GetDataStore("PointsStored")
local function retrievepoints(player)
local playeruserID = `Player`..player.UserID
local data = playerData:GetAsync(playeruserID)
if data then
player.Character:FindFirstChild("Stats").Points.Value = data[`Points`]
else
player.Character:FindFirstChild("Stats").Points.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.Character:FindFirstChild("Stats"):GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function playerexitsave(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playeruserID = `Player`..player.UserID
playerData:SetAsync(playeruserID,player_stats)
end)
end
game.Players.PlayerAdded:Connect(retrievepoints)
game.Players.PlayerRemoving:Connect(playerexitsave)
Hello, after I looked over your script I noticed that you capitalized the D in UserID, Lua is a very case-sensitive language therefore you get that error, as UserID is not the right property but UserId is. Hope this helps
additional issues idk if they are related to the issue:
Type Error: (8,3) Value of type ‘Model?’ could be nil
Type Error: (8,3) Key ‘Points’ not found in class ‘Instance’
Type Error: (10,3) Value of type ‘Model?’ could be nil’
Type Error: (10,3) Key ‘Points’ not found in class ‘Instance’
those issues are here btw:
if data then
player.Character:FindFirstChild("Stats").Points.Value = data[`Points`]
else
player.Character:FindFirstChild("Stats").Points.Value = 0
end
u can add this to fix it - checks if the character thing and stat exist:
if data then
local character = player.Character
if character then
local stats = character:FindFirstChild("Stats")
if stats then
stats.Points.Value = data["Points"]
else
warn("Stats not found in player's character")
end
else
warn("Player's character not found")
end
else
warn("Data is nil")
end
Complete Code
local MyDataStore = game:GetService("DataStoreService")
local playerData = MyDataStore:GetDataStore("PointsStored")
local function retrievepoints(player: Player)
local playeruserID = `Player`..player.UserId
local data = playerData:GetAsync(playeruserID)
if data then
local character = player.Character
if character then
local stats = character:FindFirstChild("Stats")
if stats then
stats.Points.Value = data["Points"]
else
warn("Stats not found in player's character")
end
else
warn("Player's character not found")
end
else
warn("Data is nil")
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.Character:FindFirstChild("Stats"):GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function playerexitsave(player: Player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playeruserID = `Player`..player.UserId
playerData:SetAsync(playeruserID,player_stats)
end)
end
game.Players.PlayerAdded:Connect(retrievepoints)
game.Players.PlayerRemoving:Connect(playerexitsave)
if this was not the issue then you provided the wrong information
So the data is nil that means it all works but doesnt save. And now i am done because i literally dont even know how to make the server wait and finish the save or something i was reading here.
You should set the stats folder in the player, instead of its character. That will fix the issue. Notice how the character can respawn, and that the events that youre using are when a player joins or leave.
If you add the stats on the player, this folder wont be affected all along but by the modifications that just you can control.
MyDataStore = game:GetService("DataStoreService")
local playerData = MyDataStore:GetDataStore("PointsStored")
local function retrievepoints(player)
local playeruserID = "Player"..player.UserID
local data = playerData:GetAsync(playeruserID)
local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = 0
if data then
points.Value = data["Points"]
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.Stats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function playerexitsave(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playeruserID = "Player"..player.UserID
playerData:SetAsync(playeruserID, player_stats)
end)
end
game.Players.PlayerAdded:Connect(retrievepoints)
game.Players.PlayerRemoving:Connect(playerexitsave)
I recommend adding another script that fires when the server is shutting down. Furthermore, I also recommend saving all player data under one location, say serverstorage, replicatedstorage, or workspace. This would make it easier to access and less likely to lose it.
game:BindToClose(function()
for i, player_stats in pairs(--[[location of all player data--]]) do
local success, errormessage = pcall(function()
local playerID = 'Player'..player.UserId
playerData:SetAsync(playerId, --[[data]]--)
if errormessage then warn(errormessage) end
end)
end
end)
I haven’t tested it but this should help prevent data loss.
So i managed to make another datastore v2 and it works way better somehow. But thanks for anyone who helped! EDITING IT CUZ ITS sPAMMING DATASTORE DONT WORRY
local stat = "Points"
local startamount = 0
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("PointsDatasave")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue",leader)
Cash.Name = stat
Cash.Value = ds:GetAsync(player.UserId) or startamount
ds:SetAsync(player.UserId, Cash.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Points.Value)
end)
I’m glad you have found something that works better, I’m still just worried data may not save when the last player leaves. I strongly recommend utilising the BindToClose() function to ensure all data has been saved.
You can never go wrong with being extra cautious with player data