game:GetService("ServerScriptService")-- loads services and defines variables
local DataStoreService = game:GetService("DataStoreService")
local gameDataStore = DataStoreService:GetDataStore("gameDataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(player)
	player = player
	-- creates the leaderstats folder and instances
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue",leaderstats)-- creates money
	money.Name = "money"
	money.Value = 0
	
	local strength = Instance.new("IntValue",leaderstats)-- creates strength
	strength.Name = "strength"
	strength.Value = 0
	
	local newplayer = Instance.new("IntValue", leaderstats)-- creates newplayer
	newplayer.Name = "newplayer"
	newplayer.Value = 1
	-- load data
	local money = player.leaderstats.money-- defines variables 
	local strength = player.leaderstats.strength
	local newplayer = player.leaderstats.newplayer
	
		local success, errormessage = pcall(function()
			dataDict = gameDataStore:GetAsync(player.UserId)-- part that is actually supposed to load the data
		end)
		
		if success then-- updates leaderstats values if data loading is successful
			if dataDict.moneydata ~= 0 then--checks money value from dataDict to make sure its not 0 because i dont want to load a 0
				money.Value = dataDict.moneydata
				print(money.Value)
			end
			if dataDict.strengthdata ~= 0 then--checks strength value from dataDict to make sure its not 0 because i dont want to load a 0 
				strength.Value = dataDict.strengthdata
			end
			if dataDict.newplayerdata ~= 0 then--checks newplayer value from dataDict to make sure its not 0 because i dont want to load a 0
				newplayer.Value = dataDict.newplayerdata
			end
			print("loaded data")
		else-- doesnt update leaderstats if the data loading fails
			print("big homosexual while getting data")
			warn(errormessage)
		end

end)

local function gamesave(player)
	print(player)
	local UserId = player.UserId
	
	local money = player.leaderstats.money.Value-- defines variables
	
	local strength = player.leaderstats.strength.Value
	
	local newplayer = player.leaderstats.newplayer.Value
	
	local dataDict = {-- creates dictionary that stores variables
		moneydata = money,
		strengthdata = strength,
		newplayerdata = newplayer
	}

	
	local success, errormessage = pcall(function()-- saves data
		gameDataStore:SetAsync(UserId,dataDict)
	end)

	if success then-- prints to console that it successfully saved the data
		print("data saved")
	else-- prints to console that it couldnt save the data
		print("data failed")
		warn(errormessage)
	end
end

game.Players.PlayerRemoving:Connect(function(player)-- used when there is more than 1 player
	for i, player in pairs(Players:GetPlayers()) do
		if i > 1 then
			gamesave(player)
			print(player)
		end
	end
end)

game:BindToClose(function()-- used just in case the server shuts down or the player abruptly disconnects
	for i, player in pairs(Players:GetPlayers()) do
			print(i)
			gamesave(player)
	end	
end)


1 comment

0

Line 46 sounds like it could be offensive...