Leaderstats not updating

Hello!

So I am currently making a Find The Memes game and am working on a DataStore/leaderstats system using modules, but I have encountered a few problems. The modules are called by a regular Server script. Each meme is also handled by a module, running from the server.

What the scripts are supposed to do is create the leaderstats and update them accordingly if the player has joined before. This does not work. It is also supposed to update the leaderstats by 1 each time a new meme is collected. This also does not work.

DataSaver module:

local DataSaver = {}

local DataStoreService = game:GetService("DataStoreService")
local CollectedDataStore = DataStoreService:GetDataStore("Collected")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MemeList = ReplicatedStorage:WaitForChild("Memes")

function DataSaver:CreateLeaderstats(Player) -- Connect this function to a Players.PlayerAdded event.  Make sure this comes first before LoadData is called.
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"

	local Memes = Instance.new("IntValue", leaderstats)
	Memes.Name = "Memes"
	Memes.Value = 0
	
	local MemesCollected = Instance.new("Folder", Player)
	MemesCollected.Name = "MemesCollected"
	
	--Loading the amount of memes collected
	local CollectedData
	local success, errormessage = pcall(function()
		CollectedData = CollectedDataStore:GetAsync("User "..Player.UserId)
	end)

	if success then
		Memes.Value = CollectedData
	end
end

function DataSaver:LoadData(Player) -- Connect this function to a Players.PlayerAdded event.
	--Load collected memes
	local Memes = CollectedDataStore:GetAsync("User "..Player.UserId)
	if Memes then
		for i, v in pairs(Memes) do
			local MemeFound = MemeList:FindFirstChild(v)
			if MemeFound then
				MemeFound:Clone().Parent = Player:FindFirstChild("MemesCollected")
			end
		end
	end
end

function DataSaver:SaveData(Player) -- Connect this function to a Players.PlayerRemoving event.
	--Save collected memes
	local success, errormessage = pcall(function()
		local CollectedSave = {}
		for i, meme in pairs(Player.MemesCollected:GetChildren()) do
			if meme then
				table.insert(CollectedSave, meme.Name)
			end
		end
		CollectedDataStore:SetAsync("User "..Player.UserId, CollectedSave)
	end)
	if success then
		print("Data has been successfully saved!")
	else
		print("Data has not been saved.")
		warn(errormessage)
	end
	--[[local CollectedValue = Player:WaitForChild("leaderstats"):WaitForChild("Memes").Value
	
	for _, Player in pairs(game.Players:GetPlayers()) do
		CollectedDataStore:SetAsync("User-"..Player.UserId, CollectedValue)
	end
	task.wait(3)--]]
end

return DataSaver

Server script:

local DataSaver = require(script:WaitForChild("DataSaver"), 5)

game.Players.PlayerAdded:Connect(function(Player)
	DataSaver:CreateLeaderstats(Player)
	DataSaver:LoadData(Player)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataSaver:SaveData(Player)
end)

Th code chunk in the meme module that updates the leaderstats upon being collected:

local leaderstats = Player:FindFirstChild("leaderstats")
				local Memes = leaderstats:FindFirstChild("Memes")
				local MemesCollected = Player:FindFirstChild("MemesCollected")
				print(MemesCollected:GetChildren(), name)
				local found = MemesCollected:FindFirstChild(name)
				print(found)
				if found then
					Memes.Value += 1
					local meme = instance:Clone()
					meme.Parent = MemesCollected
					meme.Value = true
				end

I am really stuck on this and any help is appreciated! If you think you know what’s going on, please let me know! Thank you and have a wonderful day! :slight_smile:

2 Likes

It might be because your parenting them to early. So instead of

local Memes = Instance.new("IntValue", leaderstats)
	Memes.Name = "Memes"
	Memes.Value = 0

maybe try

local Memes = Instance.new("IntValue")
	Memes.Name = "Memes"
	Memes.Value = 0
    Memes.Parent = leaderstats
3 Likes

Sadly, that didn’t work. :frowning:

[char limit]

1 Like

Ok so maybe instead of using a colon in the module scripts you use a dot

1 Like

That wasn’t it. Does the colon or dot really matter?

yes because it’s something to do with one passes self as parameter 1 and the other doesn’t which may be overwriting player also is good practice

1 Like

Are you getting any errors in the output?

1 Like

No, I am not. Sometimes it will print “Data has been successfully saved!” and sometimes it won’t. All that’s in my output is print statements.

Have you tried doing the same with leaderstats about parenting. So set the parent after you name it?

1 Like

Yes, I did that and replaced all my module functions that has a colon with a dot.

I don’t know if this helps or not, but here is my meme module where my prints statements come from.

local meme = {}
local BadgeService = game:GetService("BadgeService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local MemeList = ReplicatedStorage:WaitForChild("Memes")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local BadgeCollected = Remotes:WaitForChild("BadgeCollected")
local NewMemeCollected = Remotes:WaitForChild("NewMemeCollected")

function meme.Debounce(func)
	local isRunning = false
	return function(hit)
		if not isRunning then
			isRunning = true
			func(hit)
			task.wait(5)
			isRunning = false
		end
	end
end

function meme.Create(instance: BasePart, BadgeId: number, name: string, ImageId)
	instance.Touched:Connect(meme.Debounce(function(hit)
		--print("touched")
		if hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if Player then
				--print(Player.UserId)
				local success, BadgeInfo = pcall(function()
					return BadgeService:GetBadgeInfoAsync(BadgeId)
				end)
				print(success) -- Prints true
				if success then
					if BadgeInfo.IsEnabled then
						local success = BadgeService:AwardBadge(Player.UserId, BadgeId)
						NewMemeCollected:FireClient(Player, name)
						print(success) --Prints false
						
						if not success then
							BadgeCollected:FireClient(Player, name)
						end
					end
				end
				local leaderstats = Player:FindFirstChild("leaderstats")
				local Memes = leaderstats:FindFirstChild("Memes")
				local MemesCollected = Player:FindFirstChild("MemesCollected")
				print(MemesCollected:GetChildren(), name) -- Prints {} and the name of the meme
				local found = MemesCollected:FindFirstChild(name)
				print(found) --Prints nil
				if found then
					Memes.Value += 1
					local meme = instance:Clone()
					meme.Parent = MemesCollected
					meme.Value = true
				end
			end
		end	
	end))
end

return meme

your updated data saver module would be more useful tbh

1 Like
local DataSaver = {}

local DataStoreService = game:GetService("DataStoreService")
local CollectedDataStore = DataStoreService:GetDataStore("Collected")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MemeList = ReplicatedStorage:WaitForChild("Memes")

function DataSaver.CreateLeaderstats(Player) -- Connect this function to a Players.PlayerAdded event.  Make sure this comes first before LoadData is called.
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local Memes = Instance.new("IntValue")
	Memes.Name = "Memes"
	Memes.Value = 0
	Memes.Parent = leaderstats
	
	local MemesCollected = Instance.new("Folder")
	MemesCollected.Name = "MemesCollected"
	MemesCollected.Parent = Player
	
	--Loading the amount of memes collected
	local CollectedData
	local success, errormessage = pcall(function()
		CollectedData = CollectedDataStore:GetAsync("User "..Player.UserId)
	end)

	if success then
		Memes.Value = CollectedData
	end
end

function DataSaver.LoadData(Player) -- Connect this function to a Players.PlayerAdded event.
	--Load collected memes
	local Memes = CollectedDataStore:GetAsync("User "..Player.UserId)
	if Memes then
		for i, v in pairs(Memes) do
			local MemeFound = MemeList:FindFirstChild(v)
			if MemeFound then
				MemeFound:Clone().Parent = Player:FindFirstChild("MemesCollected")
			end
		end
	end
end

function DataSaver.SaveData(Player) -- Connect this function to a Players.PlayerRemoving event.
	--Save collected memes
	local success, errormessage = pcall(function()
		local CollectedSave = {}
		for i, meme in pairs(Player.MemesCollected:GetChildren()) do
			if meme then
				table.insert(CollectedSave, meme.Name)
			end
		end
		CollectedDataStore:SetAsync("User "..Player.UserId, CollectedSave)
	end)
	if success then
		print("Data has been successfully saved!")
	else
		print("Data has not been saved.")
		warn(errormessage)
	end
	--[[local CollectedValue = Player:WaitForChild("leaderstats"):WaitForChild("Memes").Value
	
	for _, Player in pairs(game.Players:GetPlayers()) do
		CollectedDataStore:SetAsync("User-"..Player.UserId, CollectedValue)
	end
	task.wait(3)--]]
end

return DataSaver
1 Like

Are the leaderstats showing up? or is it just stuck at 0

1 Like

They are stuck at zero.

[char limit]

hmmmm so is it just not finding the Meme in the meme collected folder then?

1 Like

Try making a separate script in ServerScriptService for creating leaderstats.

1 Like

@hya123456h I guess so. I was talking to another guy about this issue and he said it looked to him that the meme was never being added to the folder, and that was what was causing the problem.

@Kilpamations I had done that previously and had problems because both scripts looked the same and used similar functions and I was told it would be best to combine the two scripts.

Nvm mind the glitch is finally fixed! @FloofyNezzled has solved it! Thanks for the help!

The problem stemmed from my meme module where I put if found then when I should’ve put if not found then.

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