Hello! I am currently trying to make a Find The… game where you have to find memes that are hidden around the map. It’s almost finished, but I am trouble getting the leaderstats to work properly. Even though I have reset the DataStore
and scanned the code multiple times, nothing seems to be working. Here is my code:
DATA SAVER SERVER SCRIPT:
--Variables
local DataStoreService = game:GetService("DataStoreService")
local collectedDataStore = DataStoreService:GetDataStore("MemesCollected2000")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MemeList = ReplicatedStorage:WaitForChild("Memes")
game.Players.PlayerAdded:Connect(function(Player)
local MemesCollected = Instance.new("Folder", Player)
MemesCollected.Name = "MemesCollected"
--Load collected data
local success, errormessage = pcall(function()
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 = MemesCollected
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
--Saving 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("Your data has been saved!")
else
print("Your data hasn't been saved! Please try again later.")
warn(errormessage)
end
end)
game:BindToClose(function(Player)
--Saving 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)
end)
LEADERSTATS SERVER SCRIPT:
local DataStoreService = game:GetService("DataStoreService")
local amountCollected = DataStoreService:GetDataStore("AmountCollected")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local Collected = Instance.new("IntValue", leaderstats)
Collected.Name = "Collected"
Collected.Value = 0
local PlayerUserId = "Player_"..Player.UserId
--Loading amount collected
local amountCollectedData
local success, errormessage = pcall(function()
amountCollectedData = amountCollected:GetAsync(PlayerUserId)
end)
if success then
print("success")
Collected.Value = amountCollectedData
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local PlayerUserId = "Player_"..Player.UserId
local collectedValue = Player.leaderstats.Collected.Value
local success, errormessage = pcall(function()
amountCollected:SetAsync(PlayerUserId, collectedValue)
end)
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
local PlayerUserId = "Player_"..Player.UserId
local collectedValue = Player.leaderstats.Collected.Value
local success, errormessage = pcall(function()
amountCollected:SetAsync(PlayerUserId, collectedValue)
end)
end
end)
If you think you know what went wrong, please let me know. Thank you and have a wonderful day!