Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --> Services
- local Workspace =game:GetService('Workspace')
- local RunService =game:GetService('RunService')
- local MarketPlaceService =game:GetService('MarketplaceService')
- local DataStoreService =game:GetService('DataStoreService')
- local Players =game:GetService('Players')
- local ReplicatedStorage =game:GetService('ReplicatedStorage')
- local ReplicatedFirst =game:GetService('ReplicatedFirst')
- local ServerScriptService =game:GetService('ServerScriptService')
- local ServerStorage =game:GetService('ServerStorage')
- local StarterPack =game:GetService('StarterPack')
- local Teams =game:GetService('Teams')
- local Chat =game:GetService('Chat')
- local StarterGui =game:GetService('StarterGui')
- local DataStore = DataStoreService:GetDataStore('DataStore1')
- local Tries = 3 --> Kicks from game if cant load data within 3 tries (I don't know how to explain that anybetter)
- local DataLoaded = false
- --> Setting/ Saving the Players Data
- local function setData(Player)
- print(Player)
- --> This will only run if the player(s) data has loaded
- if DataLoaded then
- --> Variables
- local DataKey = Player.UserId
- local Count = 0
- local Data = {
- ['Fossils'] = Player.LeaderBoard.Fossils.Value,
- ['Coins'] = Player.LeaderBoard.Coins.Value,
- ['Gems'] = Player.LeaderBoard.Gems.Value
- }
- local Succsess, Error
- --> This loops to see if the players data has saved(Handles Error)
- repeat
- Succsess, Error = pcall(function()
- DataStore:SetAsync(DataKey, Data)
- end)
- Count = Count +1
- until Count >= Tries or Succsess
- --> If data didn't save prints/ warns and error(Handles error)
- if not Succsess then
- warn('Data Failded to save, Code: '.. tostring(Error))
- return
- end
- else
- return
- end
- end
- -->Loading/ Getting the players Data
- local function GetData(Player)
- print(Player:GetFullName())
- --> Variables
- local DataKey = Player.UserId
- local Count = 0 --> It hasnt began trying yet
- local Data --> An return Value for the pcall(function())
- local Success, Error
- repeat
- Success, Error = pcall(function()
- Data = DataStore:GetAsync(DataKey)
- end)
- Count = Count +1 --> We will loop until their data loads (This is were the amount of tries comes in to play)
- until Count >= Tries or Success
- --> If something went wrong while loading data the player get kick
- if not Success then
- wait()
- warn('Data failded, Error: '..tostring(Error))
- Player:Kick('Your Data has faild to load. Please rejoin to try again (Automated Kick)') --> Kicking the player because their data isnt loading or hasnt loaded within the required time(3)
- return
- end
- --> This looks for a new player And gives them new data
- if Success then
- if Data then
- return Data --> If a player has already played the game it will return there original data
- else
- return
- {
- ['Fossils'] = 0,
- ['Coins'] = 0, --> Default (FOR NEW PLAYERS)
- ['Gems'] = 0 --> Default (FOR NEW PLAYERS)
- }
- end
- end
- end
- -->Leaderboard
- local function CreatPlayerLeaderBoard(Player)
- print(Player)
- local Values = GetData(Player)
- local leaderboard = Instance.new('Folder')
- leaderboard.Name = 'LeaderBoard' --> Giving the LeaderBoard a name (Mainstats) beacuse i dont want it to shoe up
- leaderboard.Parent = Player --> Setting It's parent to the player
- local Fossil = Instance.new('NumberValue') --> Data in the players Inventory/ Wallet
- Fossil.Name = 'Fossils'
- Fossil.Parent = leaderboard
- local Coins = Instance.new('NumberValue') --> Data in the players Bank
- Coins.Name = 'Coins'
- Coins.Parent = leaderboard
- local Gems = Instance.new('NumberValue') --> Data in the players Inventory/ Wallet
- Gems.Name = 'Gems'
- Gems.Parent = leaderboard
- --> Setting Player Data Values
- Coins.Value = Values.Coins
- Gems.Value = Values.Gems
- Fossil.Value = Values.Fossils
- DataLoaded = true
- local function SaveDataOnChanged(DataValue)
- DataValue.Changed:Connect(function()
- print('Changed...')
- setData(Player)
- end)
- end
- SaveDataOnChanged(Player.LeaderBoard.Fossils)
- end
- Players.PlayerRemoving:Connect(setData)
- Players.PlayerAdded:Connect(CreatPlayerLeaderBoard)
- game:BindToClose(function()
- for i, User in next, game.Players:GetChildren() do
- if User then
- setData(User)
- end
- end
- end)
Add Comment
Please, Sign In to add comment