Why are the Glory Points and Glory Coins the same for all players?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hello! I have a game where you skydive, collection points and coins and those save.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is that all players have a fixed amount of Glory Points and Glory Coins. When I printed the value out in the output, it gave the actual value. But the leaderstats still appears fixed.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have not found much topics like this. If you have any suggestions or solutions, please list them down below.

Module

local mod = {}

local minimumPlayers = 1
local gameTime = 10

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:FindFirstChild("RemoteEvents")
local Skydive = RemoteEvents:WaitForChild("Skydive")

local SoundService = game:GetService("SoundService")
local GlobalSounds = SoundService.GlobalSounds
local CountdownSound = GlobalSounds.CountdownSound

local debounce = false


function mod:Intermission(Players, Status, Intermission, IntermissionLength)
	local function EnoughPlayers()
		for i = 0, 3, 1 do
			Status.Value = "Waiting for more players to join"..string.rep(".", i)
			task.wait(1)
		end
	end

	repeat EnoughPlayers() task.wait() until #Players:GetPlayers() >= minimumPlayers

	repeat task.wait() until #Players:GetPlayers() >= minimumPlayers
	-- set the intermission value to true
	Intermission.Value = true

	for i = IntermissionLength, 0, -1 do
		Status.Value = "Intermission: "..tostring(i)
		task.wait(1)
	end

	Intermission.Value = false
end




function mod:StartRound(Players, Status, Arena, TeleportTo)
	-- start the round
	for i, player in pairs(Players:GetPlayers()) do
		local Character = player.Character or player.CharacterAdded:Wait()
		if Character then
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			if HumanoidRootPart then
				player:SetAttribute("InRound", true)
				if player:GetAttribute("InRound") then
					HumanoidRootPart:PivotTo(TeleportTo.CFrame)
					Skydive:FireClient(player)
				end
			end
		end
	end
	
	for i = 3, 1, -1 do
		Status.Value = tostring(i).. "..."
		CountdownSound:Play()
		task.wait(1)
	end
	CountdownSound:Play()
	Status.Value = "GO!"

	Arena.BrickColor = BrickColor.random()
	task.wait(2)
	Arena.CanCollide = false
	Status.Value = "Round in progress!"
end




function mod:EndRound(Players, Status, Arena, Target, LobbySpawn, RoundEnded)
	-- end the round
	local Winners = {}
	
	local rewardedGloryPoints = math.random(100, 300)
	local rewardedGloryCoins = math.random(25, 50)
	
	local rewardChance = math.random(1, 4)
	local rewardBoost = 2
	
	local function NoWinners()
		task.wait(35)
		if #Winners == 0 then -- if there are no winners after 35 seconds.
			Status.Value = "No winners! Disbanding round!"
			if Arena.CanCollide == false then
				Arena.CanCollide = true
			end
		end
	end
	
	Target.Touched:Connect(function(Hit)
		local Player = Players:GetPlayerFromCharacter(Hit.Parent)
		
		if Player then
			if not table.find(Winners, Player) then -- if it's not the same player.
				table.insert(Winners, Player) -- then insert them into the table.
			end
			
			local Character = Player.Character or Player.CharacterAdded:Wait()
			if Character then
				local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
				if HumanoidRootPart then
					Player:SetAttribute("InRound", false)
					if not Player:GetAttribute("InRound") then
						HumanoidRootPart:PivotTo(LobbySpawn.CFrame + Vector3.new(0, 10, 0))
					end
					Arena.CanCollide = true
					for i, winner in pairs(Winners) do
						if table.find(Winners, Player) then -- checks if the player is a winner.
							if not debounce then
								debounce = true
								Status.Value = "Winner(s): "..winner.Name
								if rewardChance == 3 then
									-- doubles rewards gained from round.
									Player.leaderstats["Glory Points"].Value += rewardedGloryPoints * rewardBoost
									Player.leaderstats["Glory Coins"].Value += rewardedGloryCoins * rewardBoost
								else
									Player.leaderstats["Glory Points"].Value += rewardedGloryPoints
									Player.leaderstats["Glory Coins"].Value += rewardedGloryCoins
								end
								task.wait(3)
								debounce = false
							end
						end
						task.wait(1)
					end
					table.clear(Winners) -- clears the table of previous winners.
					RoundEnded.Value = true
					task.wait(3)
					RoundEnded.Value = false
				end
			end
		end
	end)
	
	NoWinners()
end



return mod

Normal Script

local DataStoreService = game:GetService("DataStoreService")
local Currencies = DataStoreService:GetDataStore("Currencies")


game.Players.PlayerAdded:Connect(function(Player) -- When the player joins the game.
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local GloryPoints = Instance.new("IntValue")
	GloryPoints.Name = "Glory Points"
	GloryPoints.Value = 0
	GloryPoints.Parent = leaderstats
	
	local GloryCoins = Instance.new("IntValue")
	GloryCoins.Name = "Glory Coins"
	GloryCoins.Value = 0
	GloryCoins.Parent = leaderstats
	
	local Data
	local success, errormessage = pcall(function()
		Data = Currencies:GetAsync("-globalcurrencies")
	end)
	
	if success then
		GloryPoints.Value = Data[1]
		GloryCoins.Value = Data[2]
		print("Successfully got currencies.")
	else
		warn(errormessage)
	end
end)



game.Players.PlayerRemoving:Connect(function(Player) -- When the player leaves the game.
	local leaderstats = Player.leaderstats
	
	local saveData = {}
	
	for i, currency in ipairs(leaderstats:GetChildren()) do
		table.insert(saveData, currency.Value)
	end
	
	local success, errormessage = pcall(function()
		Currencies:SetAsync("-globalcurrencies", saveData)
	end)
	
	if success then
		print("Successfully set currencies.")
	else
		warn(errormessage)
	end
end)

you need to make sure you save a different datastore for each player, right now you are only saving to “-globalcurrencies”, which means that all players will save to the same point. try adding the player’s UserId to the key.

1 Like

I made a huge mistake. Thank you for that!

How would I reset all player’s data for now? I just want everyone to restart with 0 Glory Points and 0 Glory Coins.

Assuming your testing the game and it isn’t out, just create a new datastore by naming it differently

1 Like

Now I feel dumb lol. And I knew something was missing, I just didn’t know what.

So if I want to check if the table is empty and it wasn’t cleared then can I do like this:

if #myTable == 0 and not table.clear(myTable) then
-- do something here
end

I’m awaiting a response…

Just do

if #myTable > 0  then
-- table is not fully clear
end

This depends on whether your table uses number type keys. If the table is like {1,2,3,4,5} then #myTable == 0 should work to check if the table is empty. If the table (dictionary) is like {["Age"] = 1} then #myTable will always return 0. If it’s a dictionary just loop through It and see its values to nil. This should delete the key from the dictionary

That works, but in the the round system, the game sets winners in a table, then clears the table after it’s done.

I’m trying to check if the table is empty and it wasn’t cleared.

If I just say:

local Winners = {}

if #Winners == 0 then
-- this won't work, because what if the table was cleared?
end

if #Winners == 0 and not table.clear(Winners) then
-- this will work because it's checking if there's nothing in the table and it was not cleared meaning a round took place, because after the round it clears the table of winners
end

This is for a disband system on the match. If there are no winners in 35 seconds and the table was not cleared, then it will disband.

1 Like

make a varible called tableCleared and make it true when you clear the table

1 Like

That’s exactly what I did. No seriously, I did that right before you told me. It worked.

1 Like

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