Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

leader board safing problem?

Asked by 2 years ago

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)

view source

01 local player = game.Players:FindFirstChild(otherPart.Parent.Name) 02
03 if player then 04
05 local completed_folder = player.completed 06
07 local alreadyCompleted = completed_folder:FindFirstChild(level.Name) 08
09 if not alreadyCompleted then 10
11 local level_name = Instance.new('StringValue') 12
13 level_name.Name = level.Name 14
15 level_name.Parent = completed_folder 16
17 player.leaderstats.Pogs.Value = player.leaderstats.Pogs.Value + 1 18
19 end 20
21 end end

here is my datastore script 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)

1 answer

Log in to vote
0
Answered by 2 years ago

This might be totally wrong, but I've found in the past that you need to use game:BindToClose to add some delay at the end. I found that otherwise, the server closes before it has chance to save your data and it just gets lost. I'd recommend using this: game:BindToClose(function() wait(10) end)

That might give your server enough time to save your data before it closes.

Ad

Answer this question