How do I add a data store to a leaderboard I made previously?
I am trying to add a data store to my leaderstats so the amount of points and survivals a player makes is saved so that when they join again they have the same number of points and wins as I have a shop GUI where you can buy stuff using the points. In addition, I want the players to have the gear they buy in the game to be with them when they join.
I tried to make a datastore on a separate script in the workspace but when I join my game a leaderboard comes up but it isn't the leaderboard I made previously as a player isn't receiving points when they survive on the new leaderboard.How do I make a datastore for my existing leaderboard?
This is the code for my leaderboard:
006 | feedbackEnabled = true |
007 | function feedback(parent, message) |
008 | if not feedbackEnabled then return end |
009 | local h = parent:findFirstChild( "announcement" ) |
013 | h = Instance.new( "Message" ) |
014 | h.Name = "announcement" |
020 | function removeFeedback(parent, delay) |
021 | local h = parent:findFirstChild( "announcement" ) |
025 | if h ~ = nil and h.Text = = txt then |
031 | function compilePlayers(players) |
033 | if #players = = 1 then return players [ 1 ] .Name end |
035 | if i = = #players then |
036 | names = names.. "and " .. players [ i ] .Name |
038 | names = names.. players [ i ] .Name.. ", " |
045 | function setTag(parent, type , name, value) |
047 | local tag = parent:findFirstChild(name) |
049 | if tag.className = = type then |
052 | local newTag = Instance.new( type ) |
055 | newTag.Parent = parent |
058 | local newTag = Instance.new( type ) |
061 | newTag.Parent = parent |
065 | function getTag(parent, type , name, default) |
067 | local tag = parent:findFirstChild(name) |
069 | if tag.className = = type then |
072 | print ( "No tag of the specified name and class was found in " , parent) |
076 | print ( "No tag named " , name, " found in " , parent) |
081 | function died(player, humanoid) |
082 | if player ~ = nil then |
083 | if humanoid ~ = nil then |
084 | if humanoid.Health = = 0 then |
085 | setTag(player, "BoolValue" , "survived" , false ) |
088 | setTag(player, "BoolValue" , "survived" , false ) |
095 | local players = game.Players:getChildren() |
096 | local connections = { } |
101 | if players [ i ] .Character ~ = nil then |
102 | setTag(players [ i ] , "BoolValue" , "survived" , true ) |
103 | table.insert(connections, players [ i ] .Character.Humanoid.Changed:connect( function () died(players [ i ] , players [ i ] .Character.Humanoid) end )) |
111 | players = game.Players:getChildren() |
115 | local survived = getTag(players [ i ] , "BoolValue" , "survived" , false ) |
117 | setTag(players [ i ] :findFirstChild( "leaderstats" ), "IntValue" , "Survivals" , getTag(players [ i ] :findFirstChild( "leaderstats" ), "IntValue" , "Survivals" , 0 ) + 1 ) |
118 | setTag(players [ i ] :findFirstChild( "leaderstats" ), "IntValue" , "Points" , getTag(players [ i ] :findFirstChild( "leaderstats" ), "IntValue" , "Points" , 0 ) + bin.points.Value) |
119 | table.insert(survivedt, players [ i ] ) |
126 | for i = 1 ,#connections do |
127 | connections [ i ] :disconnect() |
130 | if #survivedt > 0 then |
131 | feedback(game.Workspace, compilePlayers(survivedt).. " survived." ) |
133 | feedback(game.Workspace, "Nobody Survived!" ) |
135 | removeFeedback(game.Workspace, 3 ) |
138 | function runningChanged(run) |
139 | print ( "Running Changed" ) |
140 | if run.Value = = true then |
148 | function newPlayer(player) |
149 | local stats = Instance.new( "IntValue" ) |
150 | stats.Name = "leaderstats" |
152 | local points = Instance.new( "IntValue" ) |
153 | points.Name = "Points" |
158 | local survivals = Instance.new( "IntValue" ) |
159 | survivals.Name = "Survivals" |
162 | points.Parent = stats |
163 | survivals.Parent = stats |
164 | stats.Parent = player |
167 | game.Players.ChildAdded:connect(newPlayer) |
168 | bin.running.Changed:connect( function () runningChanged(bin.running) end ) |
169 | print ( "Leaderboard Loaded" ) |
`
This is the code for my datastore:
01 | local datastore = game:GetService( "DataStoreService" ):GetDataStore( "GameStore" ) |
04 | game.Players.PlayerAdded:connect( function (player) |
05 | local leaderstats = Instance.new( "IntValue" ) |
06 | leaderstats.Name = "leaderstats" |
07 | leaderstats.Parent = player |
09 | local points = Instance.new( "IntValue" ) |
10 | points.Name = "Points" |
11 | points.Parent = leaderstats |
13 | local survivals = Instance.new( "IntValue" ) |
14 | survivals.Name = "Survivals" |
15 | survivals.Parent = leaderstats |
17 | local key = "user-" .. player.userId |
19 | local storeditems = datastore:GetAsync(key) |
21 | points.Value = storeditems [ 1 ] |
22 | survivals.Value = storeditems [ 2 ] |
24 | local items = { points.Value, survivals.Value } |
25 | datastore:SetAsync(key, items) |
29 | game.Players.PlayerRemoving:connect( function (player) |
30 | local items = { player.leaderstats.Points.Value, player.leaderstats.survivals.Value } |
31 | local key = "user-" .. player.userId |
33 | datastore:SetAsync(key, items) |
Thank You and help would be appreciated