Leaderstats only showing 1 thing

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Cash = Instance.new("IntValue",leader)
	Cash.Name = "Berries"
	Cash.Value = ds:GetAsync(player.UserId) or 0
	ds:SetAsync(player.UserId, Cash.Value)
	Cash.Changed:connect(function()
		ds:SetAsync(player.UserId, Cash.Value)
	end)
end)



game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Cash1 = Instance.new("IntValue",leader)
	Cash1.Name = "Oranges"
	Cash1.Value = ds:GetAsync(player.UserId) or 0
	ds:SetAsync(player.UserId, Cash1.Value)
	Cash1.Changed:connect(function()
		ds:SetAsync(player.UserId, Cash1.Value)
	end)
end)

I have this script here yet it only shows the leaderstats for Oranges whys that?

Try this instead!

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:Connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue",leader)
	Cash.Name = "Berries"
	Cash.Value = ds:GetAsync(player.UserId) or 0
	
	ds:SetAsync(player.UserId, Cash.Value)
	
	local Cash1 = Instance.new("IntValue",leader)
	Cash1.Name = "Oranges"
	Cash1.Value = ds:GetAsync(player.UserId) or 0
	
	ds:SetAsync(player.UserId, Cash1.Value)
	
	Cash.Changed:Connect(function()
		ds:SetAsync(player.UserId, Cash.Value)
	end)
	
	Cash1.Changed:Connect(function()
		ds:SetAsync(player.UserId, Cash1.Value)
	end)
end)

also setting data within the changed event. Wouldn’t be a correct usage of the datastores because you get a set budget and when you surpass it the requests get queried

Resource

I tried this but it only seems to have the berries show.

I recommend storing data within a table… here’s a coded example I wrote long ago.

Hey, Try this Out.
This should be Working.

This Has DataStoring In Tables So no Issues should Occur.
I Tried explaining in the Script Itself.

  • Make Sure this Is A “Script” In “ServerScriptService”

Hope i could Help :happy3:

local DataStoreService = game:GetService("DataStoreService")
local GameStore = DataStoreService:GetDataStore("playerData")

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

-- 

Players.PlayerAdded:Connect(function(Player) -- When Player Joins
	local newFolder = Instance.new("Folder") -- Make a New Folder
	newFolder.Parent = Player  -- Parent it to Player
	newFolder.Name = "leaderstats" -- Call it Leaderstats
	
	local berriesInt = Instance.new("IntValue") -- Add A Integer Value
	berriesInt.Value = 0 -- Default Value will be 0
	berriesInt.Parent = newFolder -- Parent it to the Folder
	berriesInt.Name = "Berries" -- Call it Berries
	
	local orangeInt = Instance.new("IntValue") -- Add A Integer Value
	orangeInt.Value = 0 -- Default Value will be 0
	orangeInt.Parent = newFolder -- Parent it to the Folder
	orangeInt.Name = "Oranges" -- Call it Oranges
	
	local oldData -- Blank Var
	local foundData,newPlr = pcall(function() -- Wrap in Pcall so Script Does not Break
		oldData = GameStore:GetAsync(Player.UserId) -- Check for Data
	end)
	if foundData and oldData then -- If Data
		berriesInt.Value = oldData.Berries -- Set Data Berries
		orangeInt.Value = oldData.Oranges -- Set Data Oranges
	end
end)

Players.PlayerRemoving:Connect(function(Player) -- When Player Leaves
	local savedData,didNot = pcall(function() -- Wrap in Pcall
		GameStore:SetAsync(Player.UserId,{ -- Key Is Player's UserId and Data is Stored in Table
			Berries = Player.leaderstats.Berries.Value, -- Berries is Stored as The Berries Int
			Oranges = Player.leaderstats.Oranges.Value, -- Oranges is Stored as The Oranges Int
		})
	end)
	if not savedData then -- If An Error
		warn(didNot) -- Warn the Error in Output
	end
end)

game:BindToClose(function() -- When Server Is Closing
	if RunService:IsStudio() then -- If the Test is in ROBLOX Studio
		wait(1) -- Wait 1 Second to Save Data from Previous Function
	else -- Else
		for _, Player in pairs(Players) do -- Loop through All Players
			local UserId = Player.UserId -- Get UserId
			local savedData,didNot = pcall(function() -- Wrap In Pcall
				GameStore:SetAsync(UserId,{ -- Set the Key to Player's UserId and Store In Tables
					Berries = Player.leaderstats.Berries.Value, -- Berries is Stored as The Berries Int
					Oranges = Player.leaderstats.Oranges.Value, -- Oranges is Stored as The Oranges Int
				})
			end)
			if not savedData then -- If An Error
				warn(didNot) -- Warn the Error in Output
			end
		end
	end
end)
1 Like

yeah thanks it works!
i appreciate it

1 Like