Leaderstats not giving to player

I’m not getting the bast amount of gems (10) and water and energy (15) any idea why?

Script

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

local replicatedModules = game.ReplicatedStorage:WaitForChild("ModuleScripts")
local CalculateStats = require(replicatedModules:WaitForChild("CalculateStats"))

local remoteEvents = game.ReplicatedStorage:WaitForChild("RemoteEvents")
local levelUpRE = remoteEvents:WaitForChild("OnLevelUp")

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

function PlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local hiddenstats = Instance.new("Folder")
	hiddenstats.Name = "hiddenstats"
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 500
	cash.Parent = leaderstats
	

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = 0
	level.Parent = leaderstats

	local exp = Instance.new("IntValue")
	exp.Name = "Exp"
	exp.Value = 0
	exp.Parent = hiddenstats
	
	local seconds = Instance.new("IntValue")
	seconds.Name = "Seconds"
	seconds.Value = 00
	seconds.Parent = hiddenstats
	

	local minutes = Instance.new("IntValue")
	minutes.Name = "Minutes"
	minutes.Value = 00
	minutes.Parent = hiddenstats
	

	local water = Instance.new("IntValue")
	water.Name = "Water"
	water.Value = 15
	water.Parent = hiddenstats

	local energy = Instance.new("IntValue")
	energy.Name = "Energy"
	energy.Value = 15
	energy.Parent = hiddenstats

	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Value = 10
	gems.Parent = hiddenstats
	
	local apartments = Instance.new("IntValue")
	apartments.Name = "Apartments"
	apartments.Parent = hiddenstats
	apartments.Value = {
		"one", 
	}
	
	local success = nil
	local playerData = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)

		attempt += 1
		if not success then 
			warn(playerData)
			task.wait(3)
		end
	until success or attempt  == 5
	
	if success then
		print("Connected to database for", player.Name)
		if not playerData then
			print("Assigning default data for", player.Name)
			playerData = {
				["Cash"] = 500,
				["Gems"] = 10,
				["Water"] = 15,
				["Energy"] = 15,
				["Minutes"] = 0,
				["Seconds"] = 0,
				["Level"] = 0,
				["Exp"] = 0,
				["Apartments"] = {"one", ""}
			}
		end
		sessionData[player.UserId] = playerData
		
		else
		warn("Unable to get data for", player.UserId)
		player:Kick("Unable to load your data. Try again later")
	end
	cash.Value = sessionData[player.UserId].Cash
	cash.Changed:Connect(function()
		sessionData[player.UserId].Cash = cash.Value
	end)
	gems.Value = sessionData[player.UserId].Gems
	gems.Changed:Connect(function()
		sessionData[player.UserId].Gems = gems.Value
	end) 
	minutes.Value = sessionData[player.UserId].Minutes
	minutes.Changed:Connect(function()
		sessionData[player.UserId].Minutes = minutes.Value
	end) 
	seconds.Value = sessionData[player.UserId].Seconds
	seconds.Changed:Connect(function()
		sessionData[player.UserId].Seconds = seconds.Value
	end) 
	water.Value = sessionData[player.UserId].Water
	water.Changed:Connect(function()
		sessionData[player.UserId].Water = water.Value
	end) 
	energy.Value = sessionData[player.UserId].Energy
	energy.Changed:Connect(function()
		sessionData[player.UserId].Energy = energy.Value
	end) 
	level.Value = sessionData[player.UserId].Level
	level.Changed:Connect(function()
		sessionData[player.UserId].Level = level.Value
	end) 
	exp.Value = sessionData[player.UserId].Exp
	exp.Changed:Connect(function()
		sessionData[player.UserId].Exp = exp.Value
	end) 
	
	leaderstats.Parent = player
	hiddenstats.Parent = player
end


Players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		
	repeat
		success, errorMsg = pcall(function()
			database:SetAsync(player.UserId, sessionData[player.UserId])
		end)
		
		attempt += 1
		if not success then 
			warn(errorMsg)
			task.wait(3)
		end
		until success or attempt  == 5
	
	if success then 
		print("Data Saved for", player.Name)
		else 
		warn("Unable to save for", player.Name)
	end
	end
end
Players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	if runService:IsStudio() then
		return
	end
	
	print("Server Shutting Down...")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)			
		end)
	end
end
game:BindToClose(ServerShutdown)

What issue do you get? Does it create the value and just does not change the value or what?

1 Like

You didn’t set the parent of Leaderstats.
leaderstats.Parent = player

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