I'm trying to make a game with a leaderboard system where once you touch a part you only get a point once. But whenever you rejoin the game it resets and allows you to get it another time. And because of the datastore that is implemented this allows you to get infinite points. Is the a solution to this?
Here are the scripts in the parts
local level = script.Parent
local function level_complete(otherPart)
local player = game.Players:FindFirstChild(otherPart.Parent.Name) if player then local completed_folder = player.completed local alreadyCompleted = completed_folder:FindFirstChild(level.Name) if not alreadyCompleted then local level_name = Instance.new('StringValue') level_name.Name = level.Name level_name.Parent = completed_folder player.leaderstats.Pogs.Value = player.leaderstats.Pogs.Value + 1 end end
end
level.Touched:Connect(level_complete)
Here is the script in the datastore
local dataStoreService = game:GetService("DataStoreService") local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats")
local loaded = {}
game.Players.PlayerAdded:connect(function(player) local leaderstats = player:WaitForChild("leaderstats") if player.UserId > 0 and player.Parent then local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId) if leaderstatsData ~= "Request rejected" then if leaderstatsData then for i, stat in ipairs(leaderstats:GetChildren()) do local value = leaderstatsData[stat.Name] if value then stat.Value = value end end end loaded[player] = true end end end)
game.Players.PlayerRemoving:connect(function(player) local leaderstats = player:FindFirstChild("leaderstats") if leaderstats then if loaded[player] then local leaderstatsData = {} for i, stat in ipairs(leaderstats:GetChildren()) do leaderstatsData[stat.Name] = stat.Value end leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData) end end loaded[player] = nil end)
Anyway to fix this?
HERE IN A SCRIPT FOR THE DATASTORE
local dataStoreService = game:GetService("DataStoreService") local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats")
local loaded = {}
game.Players.PlayerAdded:connect(function(player) local leaderstats = player:WaitForChild("leaderstats") if player.UserId > 0 and player.Parent then local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId) if leaderstatsData ~= "Request rejected" then if leaderstatsData then for i, stat in ipairs(leaderstats:GetChildren()) do local value = leaderstatsData[stat.Name] if value then stat.Value = value end end end loaded[player] = true end end end)
game.Players.PlayerRemoving:connect(function(player) local leaderstats = player:FindFirstChild("leaderstats") if leaderstats then if loaded[player] then local leaderstatsData = {} for i, stat in ipairs(leaderstats:GetChildren()) do leaderstatsData[stat.Name] = stat.Value end leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData) end end
This is the datastore codeblock loaded[player] = nil end)