Data Store script not working,

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I just want to fix this script that I have for a simulator for stats, and I need to find a way for my auto saving script to put inside this script, in a way it doesnt get deleted.

image Everytime I add my auto saving script, the circle part is just removed.

  1. What is the issue? Everytime I load the leaderboard stats work completely fine. But, when I add my auto saving data script, the whole leaderboard stat (in the upper right hand) is completely deleted.

  2. What solutions have you tried so far? I’ve tried a lot of different techniques, it’s just not working. I have ask multiple friends

LEADERBOARD STATS SCRIPT:

	local stats = player:findFirstChild("leaderstats")
	
	
	if stats ~= nil then
		local BuildingBlock = stats:findFirstChild("Spree")
		local deaths = stats:findFirstChild("Deaths")
		deaths.Value = deaths.Value + 1
		BuildingBlock.Value = 0
		local killer = getKillerOfHumanoidIfStillInGame(humanoid)
		handleKillCount(humanoid, player)
	end
end

function onPlayerRespawn(property, player)
	if property == "Character" and player.Character ~= nil then
		local humanoid = player.Character.Humanoid
			local p = player
			local h = humanoid
			humanoid.Died:connect(function() onHumanoidDied(h, p) end )
	end
end

function getKillerOfHumanoidIfStillInGame(humanoid)
	local tag = humanoid:findFirstChild("creator")
	if tag ~= nil then
		
		local killer = tag.Value
		if killer.Parent ~= nil then 
			return killer
		end
	end

	return nil
end

function handleKillCount(humanoid, player)
	local killer = getKillerOfHumanoidIfStillInGame(humanoid)
	if killer ~= nil then
		local stats = killer:findFirstChild("leaderstats")
		if stats ~= nil then
			local kills = stats:findFirstChild("Kills")
			local cash = stats:findFirstChild("Cash")
			local BuildingBlock = stats:findFirstChild("Spree")
	--------------------------------------------------------------------		
			if killer ~= player then
				kills.Value = kills.Value + 1
				cash.Value = cash.Value + 100
				BuildingBlock.Value = BuildingBlock.Value + 1
				if math.floor(BuildingBlock.Value/increment) == BuildingBlock.Value/increment then
					weapons[(BuildingBlock.Value/increment)*2]:clone().Parent = killer.Character
					print("mommy says im kool aid :)")
					local ese = game.Lighting.KILLSTREK:clone()
					ese.Parent = killer.PlayerGui
					ese.Frame.TextLabel.Text = weapons[(BuildingBlock.Value/increment)*2 -1 ]	
					coroutine.resume(coroutine.create(function() wait(5) ks:remove() end))
				end
			else
				kills.Value = kills.Value - 1
		end
-------------------------------------------------------------------------------------------------
		local data
			local success, errormessage = pcall(function()
			data = myDataStore:GetAsync(player.UserId.."-Kills")
	end)
	
	if success then
		kills.Value = data
	else 
		print("Error!")
		warn(errormessage)
	end

	end
	
	end
end

-----------------------------------------------------------------------------
function findAllFlagStands(root)
	local c = root:children()
	for i=1,#c do
		if (c[i].className == "Model" or c[i].className == "Part") then
			findAllFlagStands(c[i])
		end
		if (c[i].className == "FlagStand") then
			table.insert(findAllFlagStands, c[i])
		end
	end
end
function onPlayerEntered(newPlayer)

		local stats = Instance.new("IntValue")
		stats.Name = "leaderstats"

		local kills = Instance.new("IntValue")
		kills.Name = "Kills"
		kills.Value = 0
		
		local cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Value = 0		
		
		local BuildingBlock = Instance.new("IntValue")
		BuildingBlock.Name = "Spree"
		BuildingBlock.Value = 0	

		local deaths = Instance.new("IntValue")
		deaths.Name = "Deaths"
		deaths.Value = 0

		cash.Parent = stats
		kills.Parent = stats
		deaths.Parent = stats
		BuildingBlock.Parent = stats
		while true do
			if newPlayer.Character ~= nil then break end
			wait(5)
		end

		local humanoid = newPlayer.Character.Humanoid

		humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
		newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )


		stats.Parent = newPlayer
end
game.Players.ChildAdded:connect(onPlayerEntered)```





***AUTO SAVE SCRIPT:***





```local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")



game.Players.PlayerAdded:Connect(function(player)
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Parent = leaderstats
	------------------------------------------------------ Get Data
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-Kills")
	end)
	
	
	if success then
		Kills.Value = data
	else 
		print("Error!")
		warn(errormessage)
	end
end)
---------------------------------------------- Save data
game.Players.PlayerRemoving:Connect (function(player)
	
	local  success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-Kills",player.leaderstats.Kills.Value)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("Error!")
		warn(errormessage)
	end

end)```
2 Likes

You’re creating leaderstats twice here. Once in onPlayerEntered and once in the PlayerAdded event function. This is likely why, as you’re not editing the correct leaderstats instance with the loaded statistics.

1 Like

Okay I’ll try that thanks

30
char

@SteadyOn Can I add both of these scripts into one combined script?

@SteadyOn didnt work sir

char 30

Well personally I create a leaderboard for a player when he enters / cloning script’s children , and a datastore in the same script like this, and it works well
*Note : I only use it for values like Rank (string), values like cash/kills/spree etc.
Only use this to save tools if you know what you’re doing.
Here is what I use, you can remove “Rank” or any value you don’t want in the leaderboard by simply deleting that value from the children of leaderstats (you can edit them as well) You do not have to change anything in the script at all , just change or remove the values (like IntValues or StringValues under the “leaderstats” folder)

Directory/hierarchical tree :

image

The script in ServerScriptService :

--for leaderboard values, and to save them too.

local store_key = "DATASTORE_KEY"
local data_key = "SaveData"


plr_store = game:GetService("DataStoreService"):GetDataStore(store_key)

game.Players.PlayerAdded:connect(function(new_plr)
	local stats = script.leaderstats:Clone()
	stats.Parent = new_plr
	local stat_table = stats:GetChildren()
	local save_data = plr_store:GetAsync(new_plr.userId..data_key)
	if save_data ~= nil then
		for number,data in pairs(stat_table) do
			for s_number,s_data in pairs(save_data) do
				if data.Name == s_data[1] then
					local loaded_val = s_data[2]
					if loaded_val ~= nil then
						data.Value = loaded_val
					end
				end
			end
		end
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local stat_table = plr.leaderstats:GetChildren()
	local save_table = {}
	for number,data in pairs(stat_table) do
		table.insert(save_table,{data.Name,data.Value})
	end
	plr_store:UpdateAsync(plr.userId..data_key, function() return save_table end)
end)

@XxELECTROFUSIONxX The lowercase connect is deprecated so use Connect and replace game.Players with game:GetService("Players")

2 Likes

Ah thanks for indicating that , but it should do fine still, just change it to lowercase

* I forgot to update that in my script, I made the game a long time ago.

To explain why:
Indexing the PlayersService (and other top-level services) with :GetService() is a good idea because it won’t error if the service name is changed.