My Datatstore isnt working

So I am trying to do a datastore system for my game but it isn’t working

local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")

local database = DatastoreService:GetDataStore("testData")
local sessionData = {}


function PlayerAdded(plr)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'Values'
	leaderstats.Parent = plr

	local Amount = script.Amount:Clone()
	Amount.Parent = leaderstats
	
	local Weight = script.Weight:Clone()
	Weight.Parent = leaderstats
	
	local MaxWeight = script.MaxWeight:Clone()
	MaxWeight.Parent = leaderstats
	
	local Cash = script.Cash:Clone()
	Cash.Parent = leaderstats
	
	local Hiding = script.Hiding:Clone()
	Hiding.Parent = leaderstats
	
	local succses = nil
	local plrData = nil
	local attempt = 1

	repeat
		succses, plrData = pcall(function()	
			return database:GetAsync(plr.UserId)
		end)

		attempt += 1
		if not succses then
			warn(plrData)
			task.wait(3)
		end
	until succses or attempt == 5 --tries until is succses or until it tried 5 times
	if succses then
		print("Connected to database", plr.Name)
		if not plrData then
			print("Assigning default data", plr.Name)
			plrData = {
				["Cash"] = 0,
				["MaxWeight"] = 15,
			}
		end	
		sessionData[plr.UserId] = plrData
	else
		warn("Unable to get data for", plr.UserId)
		plr:Kick("Unable to load data. We are sorry,")
	end

	Cash.Value = sessionData[plr.UserId].Cash
	MaxWeight.Value = sessionData[plr.UserId].MaxWeight

	Cash.Changed:Connect(function()
		sessionData[plr.UserId].Cash = Cash.Value
	end)
	
	MaxWeight.Changed:Connect(function()
		sessionData[plr.UserId].MaxWeight = MaxWeight.Value
	end)

	
end
Players.PlayerAdded:Connect(PlayerAdded)


function PlayerLeaving(plr)
	if sessionData[plr.UserId] then

		local succses = nil
		local errorMsg = nil
		local attempt = 1

		repeat
			succses, errorMsg = pcall(function()	
				database:SetAsync(plr.UserId, sessionData[plr.UserId])
			end)

			attempt += 1
			if not succses then
				warn(errorMsg)
				task.wait(3)
			end
		until succses or attempt == 5 --tries until is succses or until it tried 5 times

		if succses then
			print("data saved for", plr.Name)
		else
			print("couldnt save data for", plr.Name)
		end

	end
end
Players.PlayerRemoving:Connect(PlayerLeaving)

the problem is with MaxWeight (it is a NumberValue) but always when I try to set the saved MaxWeight data to the current MaxWeight (MaxWeight.Value = sessionData[plr.UserId].MaxWeight) it just makes the MaxWeight = 0 instead of 15 or the saved MaxWeight.

Any idea how to fix it.
(Tell me if you need more information)

1 Like

The problem is that when you assign the maxweight value, you need to convert it to a string, Try this:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local database = DataStoreService:GetDataStore("testData")
local sessionData = {}

function PlayerAdded(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'Values'
	leaderstats.Parent = plr

	local Amount = script.Amount:Clone()
	Amount.Parent = leaderstats
	
	local Weight = script.Weight:Clone()
	Weight.Parent = leaderstats
	
	local MaxWeight = script.MaxWeight:Clone()
	MaxWeight.Parent = leaderstats
	
	local Cash = script.Cash:Clone()
	Cash.Parent = leaderstats
	
	local Hiding = script.Hiding:Clone()
	Hiding.Parent = leaderstats
	
	local success, plrData = pcall(function()	
		return database:GetAsync(plr.UserId)
	end)

	if success then
		print("Connected to database", plr.Name)
		if not plrData then
			print("Assigning default data", plr.Name)
			plrData = {
				Cash = 0,
				MaxWeight = 15,
			}
		else
			plrData.MaxWeight = tonumber(plrData.MaxWeight) -- Convert MaxWeight to a number
		end

		sessionData[plr.UserId] = plrData

		Cash.Value = sessionData[plr.UserId].Cash
		MaxWeight.Value = sessionData[plr.UserId].MaxWeight

		Cash.Changed:Connect(function()
			sessionData[plr.UserId].Cash = Cash.Value
		end)
	
		MaxWeight.Changed:Connect(function()
			sessionData[plr.UserId].MaxWeight = MaxWeight.Value
		end)
	else
		warn("Unable to get data for", plr.UserId)
		plr:Kick("Unable to load data. We are sorry.")
	end
end

Players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(plr)
	if sessionData[plr.UserId] then
		local success, errorMsg = pcall(function()	
			database:SetAsync(plr.UserId, sessionData[plr.UserId])
		end)

		if success then
			print("Data saved for", plr.Name)
		else
			print("Could not save data for", plr.Name)
		end
	end
end

Players.PlayerRemoving:Connect(PlayerLeaving)

its still the same problem nothing changed

did you try printing MaxWeight.Value , to check if its 15 or if its 0

yea already did that it is just 0