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

Why does my DataStore script only work most of the time in game?

Asked by 6 years ago

95% of the time it works perfectly fine in game. Lets say you left "the game" with 101 Survivals. Then re-enter "the game" sometimes you get 0 for Survivals. But if you leave the game and re-enter "the game" again you get the 101 Survivals. Can someone please help me. Thank you so much!

local DataStoreService=game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore=DataStoreService:GetDataStore("Player_Data")

game.Players.PlayerAdded:connect(function(Player)
  local Stats=Instance.new('IntValue')
  Stats.Name="leaderstats"
  local survivals=Instance.new('IntValue',Stats)
  survivals.Name="Survivals"
  survivals.Parent = Stats
  Stats.Parent=Player
  local CanSave=Instance.new('BoolValue',Player) 
  CanSave.Name="CanSaveData"
  CanSave.Value=true

  local Success,Message=pcall(function() 
    return DataStore:GetAsync(tostring(Player.UserId))
end)
    if Success then 
        if DataStore~=nil then 
        Player.leaderstats.Survivals.Value=Message
    else 
        Player.leaderstats.Survivals.Value=0
    end
    else 
        Player.CanSaveData.Value=false
        Player:Kick("Your data failed to load! Please rejoin.")
    end
end)

0
Delete lines 24 and 25? MooMooThalahlah 421 — 6y

Answer this question