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
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!
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
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
@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.