Please help with something crazy that happened in my leaderstats


local datastore = game:GetService("DataStoreService")
local DATA = datastore:GetDataStore("DATA")
local TIMEDATA = datastore:GetDataStore("DATA2")
local try = 0
local waitt = 0

local function save(player)
	local playerkey = player.UserId
print(player.UserId) --says nil
	local tableToSave = {
		player.leaderstats.pets.one.Value;
		player.leaderstats.pets.two.Value;
		player.leaderstats.pets.three.Value;
		player.leaderstats.pets.four.Value;
		player.leaderstats.pets.five.Value;
		player.leaderstats.pets.six.Value;
		player.leaderstats.Yums.Value;
		player.leaderstats.Rebirth.Value;
		player.leaderstats.burger.Value;
		player.warnings.Value;
		player.leaderstats.slimes.Value;
		game.Workspace.badges.Value;
	}
	local success, err = pcall(function()
		DATA:SetAsync(playerkey, tableToSave)
	end)

	if success then
		print("saved")
	else
		print("errrrrrrrrror")
		print(err)
		save(player)
	end
end

game.Players.PlayerAdded:connect(function(player)
	--{0.629, 0},{-0.001, 0}
	local dataplayer = DATA:GetAsync(player.UserId)
	local playerkey = player.UserId
	print("stats")
	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"
	leader.Parent = player  

	local pet = Instance.new("NumberValue")
	pet.Name = "petvalue"
	pet.Parent = player
	pet.Value = 0
	print("pet value loaded")
	
	local booly = Instance.new("BoolValue")
	booly.Name = "booly"
	booly.Parent = player
	booly.Value = false
	
	local p = Instance.new("IntValue")
	p.Name = "spamrule"
	p.Parent = player
	
	local ping = Instance.new("IntValue")
	ping.Name = "pings"
	ping.Parent = player
	ping.Value = 0
	
	local conr = Instance.new("IntValue")
	conr.Name = "connectrange"
	conr.Parent = player
	conr.Value = 1
	
	local con = Instance.new("IntValue")
	con.Name = "connect"
	con.Parent = player
	con.Value = 0
	
	local lag = Instance.new("IntValue")
	lag.Name = "lag"
	lag.Parent = player
	lag.Value = 0
	
	local m = Instance.new("IntValue")
	m.Name = "warnings"
	m.Parent = player
	
	local m2 = Instance.new("IntValue")
	m2.Name = "smallwarnings"
	m2.Parent = player
	
	local v = Instance.new("BoolValue")
	v.Name = "locked"
	v.Parent = player
	v.Value = false
	
	local Yums = Instance.new("IntValue") 
	Yums.Name = "Yums"
	Yums.Parent = leader
	print("Yums loaded")
	print(script.Parent)
	
	local sticks = Instance.new("IntValue") 
	sticks.Name = "sticks"
	sticks.Parent = player
	sticks.Value = 0
	
	local Alienslimes = Instance.new("IntValue") 
	Alienslimes.Name = "slimes"
	Alienslimes.Parent = leader
	print("Slimes loaded")
	print(script.Parent)

	local burger = Instance.new("IntValue") 
	burger.Name = "burger"
	burger.Parent = leader
	print("burger level loaded")

	local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirth"
	rebirth.Parent = leader
	print("rebirths")

	local pets = Instance.new("Folder")
	pets.Name = "pets"
	pets.Parent = leader

	local petone = Instance.new("IntValue")
	petone.Parent = pets
	petone.Name = "one"
	print("pet1 loaded")
	
	local event2 = Instance.new("BoolValue")
	event2.Name = "event2"
	event2.Parent = player
	event2.Value = false
	
	local event = Instance.new("BoolValue")
	event.Name = "event"
	event.Parent = player
	event.Value = false

	local pettwo = Instance.new("IntValue")
	pettwo.Parent = pets
	pettwo.Name = "two"
	print("pet2 loaded")

	local petthree = Instance.new("IntValue")
	petthree.Parent = pets
	petthree.Name = "three"
	print("pet3 loaded")

	local petfour = Instance.new("IntValue")
	petfour.Parent = pets
	petfour.Name = "four"
	print("pet4 loaded")

	local petfive = Instance.new("IntValue")
	petfive.Parent = pets
	petfive.Name = "five"
	print("pet5 loaded")

	local petsix = Instance.new("IntValue")
	petsix.Parent = pets
	petsix.Name = "six"
	print("pet6 loaded")
	save()
	local data
	
	local suc, err = pcall(function()
		data = DATA:GetAsync(player.UserId)
	end)	

	if suc and data then
		petone.Value = data[1]
		pettwo.Value = data[2]
		petthree.Value = data[3]
		petfour.Value = data[4]
		petfive.Value = data[5]
		petsix.Value = data[6]
		Yums.Value = data[7]
		rebirth.Value = data[8]
		burger.Value = data[9]
		m.Value = data[10]
		Alienslimes.Value = data[11]
		game.Workspace.badges.Value = data[12]
	else
		print("the player has no data")
	end
	
	if player.warnings.Value == 3 then
		if player.Name == "Mrtgr33" then
			player.warnings.Value = 0
		end
		if player.Name ~= "Player2" then
		player:Kick("You are perm-banned so you can't play this game.. You can try to contact Mrtgr33 with messages on roblox/roblox devforum to ask for an unban")
		end	
		end 
end)




game.Players.PlayerRemoving:Connect(function(player) 
	local success, err  = pcall(function()
		if tonumber(player.UserId) <= 0 then
			print("negative id")
			player.leaderstats.Yums.Value = 0
			save(player)
		else
			save(player)
		end
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!"..err)
		if tonumber(player.UserId) <= 0 then
			print("negative id")
			player.leaderstats.Yums.Value = 0
			save(player)
		else
			save(player)
			end
	end
end)


game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do 
		local success, err  = pcall(function()
			if tonumber(player.UserId) <= 0 then
				print("negative id")
				player.leaderstats.Yums.Value = 0
				save(player)
			else
				save(player)
			end
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
			if tonumber(player.UserId) <= 0 then
				print("negative id")
				player.leaderstats.Yums.Value = 0
				save(player)
			else
				save(player)
			end
		end
	end
	
end)

this is my leaderstats script worked good for a long time, but I wanted to make a teleport when loading in data fails directly back to the game with a screen saying ‘loading data failed (automatic rejoin)’. Not that I have a lot of problems with my data but just to make sure people don’t leave because of problems but instead rejoin automatically (and then hope it works).

but okay now the problem where I need help for.

I had adjusted my leaderstats on that teleport at that time. When I went to test I was shocked because the leaderstats now saw the player as nil which was not before so the data is not being retrieved.

thank you in advance for the help. I am curious how I can solve this.

By the way, I am very disappointed because it was a very good way of saving data in my experience.

1 Like

you’re calling the save function without a player argument onthe line you do save() in the function connected to PlayerAdded, so player in save() will be nil

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.