Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

data isnt saving when leaving?

my data for a game im working on isnt saving or not loading not sure. ive checked in studio and in the actual game and its not working

local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave")

game.Players.PlayerAdded:Connect(function(player)


local leaderstats = Instance.new("Folder")
leaderstats.Name= "leaderstats"
leaderstats.Parent = player

local wheat = Instance.new("NumberValue")
wheat.Name = "Wheat"
wheat.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 wheatData, rebirthsData

local success, errormessage = pcall (function()
	wheatData = DataStore:GetAsync("wheat-")..player.UserId
	rebirthsData = DataStore:GetAsync("rebirths-")..player.UserId
end)

if success then
	if wheatData then
		wheat.Value = wheatData
		rebirths.Value = rebirthsData
	end
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
	DataStore:SetAsync("wheat-"..player.UserId,player.leaderstats.Wheat.Value)
	DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.rebirths.Value)
end)
end)

2 comments

0

have you tried using prints to debug or looked at the output for errors?

0

Hey i edited the thing now u just can copy and paste the script into serverscriptservice

1 answer

don't use that use this instead

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player) -- Runs when players join

local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

leaderstats.Name = "leaderstats"

leaderstats.Parent = player



local money = Instance.new("IntValue") --Sets up value for leaderstats

money.Name = "Wheat"

money.Parent = leaderstats

local shards = Instance.new("IntValue") --Sets up value for leaderstats

RB.Name = "Rebirths"

RB.Parent = leaderstats


local playerUserId = "Player_" .. player.UserId  --Gets player ID

local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

if data then

	money.Value = data['Wheat']
	
	RB.Value = data['Rebirths']
	
	print('Dataloaded!')

else

	-- Data store is working, but no current data for this player

	money.Value = 0
	
	RB.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)

This discussion is preserved as it was. New answers, comments, and votes are closed.