I need help with this script

how do I make it in leaderstats

In serverscript service, where you created the leaderstats, could you share that script? The code would go in there for saving and loading.

game.Players.PlayerAdded:Connect(function(plr)
	local Stats = Instance.new("Folder",plr)
	Stats.Name = "leaderstats"
	Stats.Parent = plr
	
	local Coins = Instance.new("NumberValue",Stats)
	Coins.Name = "Coins"
	Coins.Parent = Stats
	Coins.Value = 0
	
	local DJumps = Instance.new("NumberValue",plr)
	DJumps.Name = "DJumps"
	
	local MaxJumps = Instance.new("NumberValue",DJumps)
	MaxJumps.Name = "MaxDJumps"
	MaxJumps.Value = 1
	
	local Boost = Instance.new("NumberValue",DJumps)
	Boost.Name = "Boost"
	Boost.Value = MaxJumps.Value
	
	local Need = Instance.new("NumberValue",DJumps)
	Need.Name = 'Need'
	Need.Value = 100
	
	local strength = Instance.new("NumberValue", Stats)
	strength.Name = "Strength"
	strength.Parent = Stats
	strength.Value = 0
	
	local diamonds = Instance.new("NumberValue", Stats)
	diamonds.Name = "Diamonds"
	diamonds.Parent = Stats
	diamonds.Value = 0
	
	local Rank = Instance.new("StringValue", Stats)
	Rank.Name = "Rank"
	Rank.Parent = Stats
	Rank.Value = "MiniFlyWeight"

thank you

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

function GetSaved(plr)
	for i,v in pairs(script:GetChildren()) do 
		local d = DataStore:GetDataStore(v.Name)
		local x = Instance.new("NumberValue",script)
		x.Name = v.Name
		x.Value = d:GetAsync(plr.UserId) or v.Value
	end
end

function Saved(player)
	for i,v in pairs(script:GetChildren()) do 
		local d = DataStore:GetDataStore(v.Name)
		d:SetAsync(player.UserId, [v.Name].Value)
	end
end

local function playerAdded(plr)
	plr.CharacterAppearanceLoaded:Connect(function(character)
		if plr and plr.Character and plr.Character == character then	
			GetSaved(plr)
			
			game:BindToClose(function()
				Saved(plr)
			end)
		end
	end)
end

for _, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)

game.Players.PlayerRemoving:Connect(function(player)
	Saved(player)
end)

Iā€™m not sure if it works or not because I didnā€™t try it, but you can test it if there is a problem tell me

Make sure the Api Servers enabled

local capacity = Instance.new("NumberValue")
	capacity.Name = "Capacity"
	capacity.Parent = Stats
	capacity.Value = game.Workspace.Script.Capacity.Value

the capacity value is that right

@Official_ProGamerYT Whatā€™s ā€œStatsā€?

it is short for leaderstats that is how my leaderstats is written

I mean where are you parenting the stats?

in the leaderstats folder i think

Should work, donā€™t know since I havenā€™t tested.

local DS = game:GetService("DatastoreService")
local CS = DS:GetDataStore("CapacityStore")

game.Players.PlayerAdded:Connect(function(player)
    local Capacity = Instance.new("IntValue", player)
    Capacity.Name = "Capacity"
    Capacity.Value = CS:GetAsync(player.UserId) or 0

    game.Players.PlayerRemoving:Connect(function)
        local success, err = pcall(function()
            CS:SetAsync(player.UserId, Capacity.Value)
        end)
    end)

    game:BindToClose(function()
        for i, plr in ipairs(game.Players:GetPlayers()) do
            local success, err = pcall(function()
                CS:SetAsync(plr.UserId, plr.Capacity.Value)
            end)
        end
    end)
end)

Fixed BindToClose()

In your original scripts, Capacity is parented to the player and strength is parented to leaderstats, or Stats.

Take This Model to make your work easier,
if u want to add more values what you need to do is just duplicate number value,then change the name of value to what you want and then change the value to how much do you want and everything should work fine

the script for the Capacity is in workspace that has the value

script.AddPoints.OnServerEvent:Connect(function(player)
	
	local amount = 1

	if player.leaderstats.Strength.Value >= script.Capacity.Value then
		player.leaderstats.Strength.Value = script.Capacity.Value

	else
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + amount
	end
end)

is there anyway to put this in leaderstats script then when i change the value it changes the capacity value

thank you @JavaKingz appreciate it and thank you @RyloRiz

1 Like

@JavaKingz where do I put the script I put in ServerScriptService and I came up with error I turned on api services and it still comes up with errors

What is the error output? A screenshot would be helpful.

image

Could you show me the expanded hierarchy of SSS? It seems theres a problem there.

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


script.AddPoints.OnServerEvent:Connect(function(player)
	local amount = 1

	if player.leaderstats.Strength.Value >= script.Capacity.Value then
		player.leaderstats.Strength.Value = script.Capacity.Value

	else
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + amount
	end
end)

function GetSaved(plr)
	for i,v in pairs(script:GetChildren()) do 
		local d = DataStore:GetDataStore(v.Name)
		local x = Instance.new("NumberValue",plr.leaderstats)
		x.Name = v.Name
		x.Value = d:GetAsync(plr.UserId) or v.Value
	end
end

function Saved(player)
	for i,v in pairs(script:GetChildren()) do 
		local d = DataStore:GetDataStore(v.Name)
		d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
	end
end

local function playerAdded(plr)
	plr.CharacterAppearanceLoaded:Connect(function(character)
		if plr and plr.Character and plr.Character == character then	
			local leader = Instance.new("Folder",plr)
			leader.Name = "leaderstats"
			GetSaved(plr)

			game:BindToClose(function()
				Saved(plr)
			end)
		end
	end)
end

for _, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end

Players.PlayerAdded:Connect(playerAdded)

game.Players.PlayerRemoving:Connect(function(player)
	Saved(player)
end)