So first im not a scripter but im trying to work for it! I am making my first game and I have watched AlvinBlox tutorial on how to make simulator games but I’m now stuck on the saving the game part where it save all the paper (wich is my kind of money in the game)and the other stuff in the leaderstats.
I have tried for many hour to understand what was wrong in my codes but since I have 0 experience in coding lua I found nothing and I need help. Here is my leaderstats codes. thanks
local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local paper = Instance.new("NumberValue")
paper.Name = "Paper"
paper.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local dataFolder = Instance.new("Folder")
dataFolder.Name = player.Name
dataFolder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = dataFolder
local paperData, rebirthsData
local success,errormessage = pcall(function()
paperData = DataStore:GetAsync("paper-"..player.UserId)
rebirthsData = DataStore:GetAsync("rebirths-"..player.UserId)
end)
if success then
if paperData then
paper.Value = paperData
rebirths.Value = rebirthsData
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync("paper-"..player.UserId,player.leaderstats.Paper.Value)
DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
end)
end)
sorry for the english error I make I am french and make mistake a lot of the time but im working for it!
yes I have tested it in the real game and I should write it like that?
game.Players.PlayerRemoving:Connect(function(player)
if success then
local success, errormessage = pcall(function()
DataStore:SetAsync("paper-"..player.UserId,player.leaderstats.Paper.Value)
DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
end)
end)
sorry im really new at coding and for the moment I just copied and paste what I saw in the tutorial I was watching but in my game it doesnt work even if im almost sure I wrote the same thing
The if paperdata is there most likely there to check if the player actually has data, so it prevents it from throwing an error about trying to set a value to nil
if success then
paper.Value = paperData
rebirths.Value = rebirthsData
end
end
end) ---this end) is underlined by red
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync("paper-"..player.UserId,player.leaderstats.Paper.Value)
DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
end)
if success then
print("Success")
else
print("Failed")
end
end)
I think you need to make a table or another DataStore to save more than one item. After saving the paper, the rebirth part overwrote it, so the game does not know the value of paper.
I know that if you are testing this in studio, the game could be closing faster than the data can save. I’m not too sure about the speed at which a non-studio server shuts down though. In that case, you would need a bind to close function
I’ve encountered a similar issue in the past. When a player leaves the game, their Player object is quickly destroyed after the event has fired. As your leaderstats are linked to the player object, they are deleted. SOLUTION: Do not store your leaderstats on the Player objects.
You can store leaderstats wherever you like. I’d personally recommend putting them in ServerScriptService. Make a folder for each player with the folder’s name being the player’s name.
Just about any location will work fine.
However, ServerStorage specifically is better suited for items that will not be edited, such as models waiting to be spawned. Leaderstats are regularly changed and therefore should not be placed in ServerStorage.
ServerScriptService contains many active objects (such as scripts) and therefore makes a great place for leaderstats.
But I thought the player can’t see items in ServerStorage and ServerScriptService.
I usually put stuff in ReplicatedStorage so both the player and the server can see it.