Can't save player's level / kills

I have this leveling system that let’s you level up if you have enough kills, and after you reach the max kills you level up and have to get more kills in order to level up again it’s basically a normal level up system but with kills.

My problem is that the kills and levels are not saving and i just simply can’t find an problem with it, it warns me that it can’t load player’s data but since i used an pcall function the rest works just fine without any errors.

The code is here:

local Players       = game:GetService("Players")
local RS            = game:GetService("ReplicatedStorage")
local DSS           = game:GetService("DataStoreService")
local VDB           = DSS:GetDataStore("Data")
local Handler       = RS:WaitForChild("Handler")

local function SaveValues(localPlayer)
	local ValuesToSave = {
		localPlayer:WaitForChild("leaderstats"):WaitForChild("Level").Value;
		localPlayer:WaitForChild("leaderstats"):WaitForChild("Kills").Value;
	}
	pcall(function()
		VDB:SetAsync(localPlayer.UserId)
	end)
end

local function debug(error)
	if error ~= nil then
		warn(error)
	end
end

local function NewServerPlayed(Player)
	local data
	
	local s, e = pcall(function()
		data = VDB:GetAsync(Player.UserId)
	end)
	
	if not s then debug("Failed to locate data, id: "..Player.UserId) end
	
	
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	local Level = Instance.new("IntValue", leaderstats)
	local Kills = Instance.new("IntValue", leaderstats)
	Level.Name = "Level"
	Kills.Name = "Kills"
	
	if data and data[1] then
		Level.Value = data[1]
	else
		Level.Value = 1
	end
	
	if data and data[2] then
		Kills.Value = data[2]
	else
		Kills.Value = 0
	end
	

	
	
	Kills:GetPropertyChangedSignal("Value"):Connect(function()
		local maxKills = 150*Level.Value
		if Kills.Value >= maxKills then
			Level.Value += 1
			Handler:FireClient(Player, "NewLevel", Kills.Value)
		elseif Kills.Value <= maxKills then
			Handler:FireClient(Player, "KillsChange", Kills.Value)
		end
	end)
	
	Handler:FireClient(Player, "KillsChange", Kills.Value)
end

local function PlayerRemoving(Player)
	SaveValues(Player)
end

for _, Player in ipairs(Players:GetChildren()) do
	NewServerPlayed(Player)
end

Players.PlayerAdded:Connect(NewServerPlayed)
Players.PlayerRemoving:Connect(PlayerRemoving)
1 Like

you forgot to actually pass the table into the save part

VDB:SetAsync(localPlayer.UserId, ValuesToSave)

1 Like

It’s still not working…

But here i did it:

local function SaveValues(localPlayer)
	local ValuesToSave = {
		localPlayer:WaitForChild("leaderstats"):WaitForChild("Level").Value;
		localPlayer:WaitForChild("leaderstats"):WaitForChild("Kills").Value;
	}
	pcall(function()
		VDB:SetAsync(localPlayer.UserId, ValuesToSave)
	end)
end

It’s not loading my data

1 Like

Turns out roblox wasn’t saving because today i tried the game and it worked!

could you mark my response as the solution rather than your own? people might yell at you for solution farming

I understand that they would but it’s still not the correct solution so i don’t feel like doing it.

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