local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local function playerAdded(player)
	local TycoonSaveFolder = Instance.new ("Folder")
	TycoonSaveFolder.Name = "TycoonSaveFolder"
	
	local leaderstats = Instance.new ("Folder")
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("IntValue")

	money.Name = "Money"
	money.Parent = leaderstats
	
	local points = Instance.new("IntValue")
	
	points.Name = "Points"
	points.Parent = leaderstats
	
	local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirths"
	rebirth.Parent = leaderstats
	leaderstats.Parent = player
	
	local playerUserId = 'Player_'..player.UserId
	local data = playerData:GetAsync(playerUserId)
	if data then
		money.Value = data["Money"]
		points.Value = data["Points"]
		rebirth.Value = data["Rebirths"]
	else
		money.Value = 0
		points.Value = 0
		rebirth.Value = 0
	end
end

local function create_table(player)
	local player_stats = {}
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserID = 'Player_'..player.UserId
		playerData:SetAsync(playerUserID, player_stats)
	end)
	
	if not success then
		warn("Could not save data")
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(onPlayerExit)

everything is working except for the

	local TycoonSaveFolder = Instance.new ("Folder")
	TycoonSaveFolder.Name = "TycoonSaveFolder"

8 comments

0

Instance.new() doesn't assign a Parent by itself. You should assign the Instance's Parent after you assign every other wanted value.

0

u forgot to assign the parent

0

but how come the leaderstats folder can be created?

0

ohh and how do you assign a player as a parent?

0

Type: TycoonSaveFolder.Parent = player

0

thank youu!! :D

0

Another way to assign the parent would be: local TycoonSaveFolder = Instance.new ("Folder",(put what you want TycoonSaveFolder's parent to be in here))

0

thanks :D