Answered by
2 years ago Edited 2 years ago
There's a lot of problems in your script that I'm too lazy to list all. Here's the script, just observe this script and the previous one to learn your mistakes.
01 | local plr = game:GetService( "Players" ) |
02 | local DSS = game:GetService( "DataStoreService" ) |
03 | local MyData = DSS:GetDataStore( "MyData1" ) |
04 | local RunService = game:GetService( "RunService" ) |
06 | local function waitForRequestBudget(requestType: Enum.DataStoreRequestType) |
07 | local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType) |
09 | while currentBudget < 1 do |
10 | currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType) |
15 | function loadData(player) |
17 | local text = player:WaitForChild( "PlayerGui" ):WaitForChild( "ScreenGui" ).TextLabel |
18 | text.Text = player.Name |
20 | local leaderstats = Instance.new( "Folder" ) |
21 | leaderstats.Name = "leaderstats" |
22 | leaderstats.Parent = player |
23 | local Coins = Instance.new( "IntValue" ) |
25 | Coins.Parent = leaderstats |
28 | local PlrId = "Player_" ..player.UserId |
31 | waitForRequestBudgetType(Enum.DataStoreRequestType.GetAsync) |
32 | local success, data 1 = pcall ( function () |
33 | return MyData:GetAsync(PlrId) |
50 | function saveData(player, dontWait) |
51 | local PlrId = "Player_" ..player.UserId |
52 | local plrdata = plr:WaitForChild( "leaderstats" ):WaitForChild( "Coins" ).Value |
56 | waitForRequestBudgetType(Enum.DataStoreRequestType.SetAsync) |
58 | success = pcall ( function () |
59 | MyData:SetAsync(PlrId, plrdata) |
64 | for _, player in ipairs (plr:GetPlayers()) do |
65 | task.spawn(loadData, player) |
68 | plr.PlayerAdded:Connect(loadData) |
69 | plr.PlayerRemoving:Connect(saveData) |
70 | game:BindToClose( function () |
71 | if RunService:IsStudio() then |
74 | local finished = Instance.new( "BindableEvent" ) |
75 | local allPlayers = Players:GetPlayers() |
76 | local leftPlayers = #allPlayers |
78 | for _,player in ipairs (allPlayers) do |
80 | saveData(player, true ) |
82 | if leftPlayers = = 0 then |