Both print statements print, however it does not work. Any idea why?
local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("testing")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local points = Instance.new("IntValue",leaderstats)
points.Name = "Points"
local kills = Instance.new("IntValue")
kills.Parent=leaderstats
kills.Name="Kills"
local playerid = "Player"..player.UserId
local data
local success, errormessage = pcall(function()
data = datastore:GetAsync(playerid)
end)
if success then
if data then
print ("works on the loading side")
points.Value = data.Points
kills.Value = data.Kills
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerid = "Player"..player.UserId
local data = {Points = player.leaderstats.Points.Value
, Kills = player.leaderstats.Kills.Value}
local success, errormessage = pcall(function()
datastore:SetAsync(playerid,data)
end)
if success then
print ("success")
end
end)
That could be the problem. datastore:SetAsync(userId, data) indeed expects a userId… but you’re passing in a string… instead, try simply passing in the player’s UserId instead of “playerid” which is “Player1234567890”. See if that works…
It doesn’t expect a user ID (at least, not at the point). It instead expects a key, which could be any number or string. Hence, "Player"..player.UserId is acceptable
epicsomepuppy, can you please print out the values of data both after line 16, and after line 28/29, to check that the value of data is what you’d expect?
Also, how are you chaning the value of Points and Kills? Is it being changed by a Server Script? If not, then I suspect the values aren’t actually being changed on the server, and instead are only being changed on the client side (hence, it won’t save)
It’s in a server script and here’s what I found:
The initial value is what you’d expect, however I attempted changing my points to 5 and then left the game and points still popped up as 0.
I’m pretty sure this shouldn’t be happening
I went under the “points” value under leaderstats and changed the value to 5
I added the print statements on line 31 and line 17 as requested
Thus
local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("testing")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local points = Instance.new("IntValue",leaderstats)
points.Name = "Points"
local kills = Instance.new("IntValue")
kills.Parent=leaderstats
kills.Name="Kills"
local playerid = "Player"..player.UserId
local data
local success, errormessage = pcall(function()
data = datastore:GetAsync(playerid)
end)
print(data)
if success then
if data then
print ("works on the loading side")
points.Value = data.Points
kills.Value = data.Kills
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerid = "Player"..player.UserId
local data = {Points = player.leaderstats.Points.Value
, Kills = player.leaderstats.Kills.Value}
print(data)
local success, errormessage = pcall(function()
datastore:SetAsync(playerid,data)
end)
if success then
print ("success")
end
end)
So you did that in Studio? When you change those values through Explorer/Properties while playing through Studio, it actually only changes it client side, hence why it isn’t be replicated to the server and subsequently isn’t saved.
to change it to the server’s view. Then, you can try modifying the values through explorer / properties are you were before, and this time it should change on the server side and save.