Savable leaderboard won't save? Please Help
I was trying to make this leaderboard savable. Player suppose to see their score even after they quit but for some reason, it won't work.
I wrote the script which is why obviously I am asking for help because its not working. So stop saying I didn't wrote the script. Yes, I used free models to base my script but that doesn't mean I didn't wrote it. Yes I was talking to you @eLunate If you can't help please let others help me instead.
Note: I noted in here where there might be a problem. I just can't figure which one.
001 | function onXPChanged(player, XP, level) |
002 | if XP.Value> = level.Value * 10 then |
004 | level.Value = level.Value + 1 |
008 | function onLevelUp(player, XP, level) |
009 | local m = Instance.new( "Hint" ) |
010 | m.Parent = game.Workspace |
011 | m.Text = player.Name .. " has leveled up!" |
014 | player.Humanoid.Health = 0 |
018 | function onPlayerRespawned(player) |
021 | player.Character.Humanoid.Health = player.Character.Humanoid.Health + player.leaderstats.Level * 10 |
023 | player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + player.leaderstats.Level * 10 |
027 | local datastore = game:GetService( "DataStoreService" ) |
028 | local ds 1 = datastore:GetDataStore( "KillsSaveSystem" ) |
029 | local ds 2 = datastore:GetDataStore( "CashSaveSystem" ) |
030 | local ds 3 = datastore:GetDataStore( "DeathSaveSystem" ) |
031 | local ds 4 = datastore:GetDataStore( "LevelSaveSystem" ) |
032 | local ds 5 = datastore:GetDataStore( "XPSaveSystem" ) |
034 | function onPlayerEntered(newPlayer) |
036 | local stats = Instance.new(newPlayer) |
037 | stats.Name = "leaderstats" |
039 | local cash = Instance.new( "IntValue" ,stats) |
042 | local kills = Instance.new( "IntValue" ,stats) |
045 | local deaths = Instance.new( "IntValue" ,stats) |
046 | deaths.Name = "Deaths" |
048 | local level = Instance.new( "IntValue" ,stats) |
051 | local xp = Instance.new( "IntValue" ,stats) |
054 | kills.Value = ds 1 :GetAsync(newPlayer.UserId) or 0 |
055 | ds 1 :SetAsync(newPlayer.UserId, kills.Value) |
057 | cash.Value = ds 2 :GetAsync(newPlayer.UserId) or 0 |
058 | ds 2 :SetAsync(newPlayer.UserId, cash.Value) |
060 | deaths.Value = ds 3 :GetAsync(newPlayer.UserId) or 0 |
061 | ds 3 :SetAsync(newPlayer.UserId, deaths.Value) |
063 | level.Value = ds 4 :GetAsync(newPlayer.UserId) or 0 |
064 | ds 4 :SetAsync(newPlayer.UserId, level.Value) |
066 | xp.Value = ds 5 :GetAsync(newPlayer.UserId) or 0 |
067 | ds 5 :SetAsync(newPlayer.UserId, xp.Value) |
070 | stats.Parent = newPlayer |
072 | deaths.Parent = stats |
076 | xp.Changed:connect( function () onXPChanged(newPlayer, xp, level) end ) |
077 | level.Changed:connect( function () onLevelUp(newPlayer, xp, level) end ) |
079 | kills.Changed:connect( function () |
080 | ds 1 :SetAsync(newPlayer.UserId, kills.Value) |
083 | cash.Changed:connect( function () |
084 | ds 2 :SetAsync(newPlayer.UserId, cash.Value) |
087 | deaths.Changed:connect( function () |
088 | ds 3 :SetAsync(newPlayer.UserId, deaths.Value) |
091 | level.Changed:connect( function () |
092 | ds 4 :SetAsync(newPlayer.UserId, level.Value) |
094 | xp.Changed:connect( function () |
095 | ds 5 :SetAsync(newPlayer.UserId, xp.Value) |
100 | local humanoid = newPlayer.Character.Humanoid |
102 | humanoid.Died:connect( function () onHumanoidDied(humanoid, newPlayer) end ) |
104 | newPlayer.Changed:connect( function (property) onPlayerRespawn(property, newPlayer) end ) |
107 | stats.Parent = newPlayer |
111 | function Send_DB_Event_Died(victim, killer) |
112 | local killername = "unknown" |
113 | if killer ~ = nil then killername = killer.Name end |
114 | print (victim.Name, " was killed by " , killername) |
116 | if shared [ "deaths" ] ~ = nil then |
117 | shared [ "deaths" ] (victim, killer) |
118 | print ( "Death event sent." ) |
122 | function Send_DB_Event_Kill(killer, victim) |
123 | print (killer.Name, " killed " , victim.Name) |
124 | if shared [ "kills" ] ~ = nil then |
125 | shared [ "kills" ] (killer, victim) |
126 | print ( "Kill event sent." ) |
132 | function onHumanoidDied(humanoid, player) |
133 | local stats = player:findFirstChild( "leaderstats" ) |
135 | local deaths = stats:findFirstChild( "Deaths" ) |
136 | deaths.Value = deaths.Value + 1 |
138 | local killer = getKillerOfHumanoidIfStillInGame(humanoid) |
141 | Send_DB_Event_Died(player, killer) |
142 | handleKillCount(humanoid, player) |
146 | function onPlayerRespawn(property, player) |
148 | if property = = "Character" and player.Character ~ = nil then |
149 | local humanoid = player.Character.Humanoid |
152 | humanoid.Died:connect( function () onHumanoidDied(h, p) end ) |
156 | function getKillerOfHumanoidIfStillInGame(humanoid) |
158 | local tag = humanoid:findFirstChild( "creator" ) |
162 | local killer = tag.Value |
163 | if killer.Parent ~ = nil then |
171 | function handleKillCount(humanoid, player) |
172 | local killer = getKillerOfHumanoidIfStillInGame(humanoid) |
173 | if killer ~ = nil then |
174 | local stats = killer:findFirstChild( "leaderstats" ) |
176 | local kills = stats:findFirstChild( "Kills" ) |
177 | local cash = stats:findFirstChild( "Cash" ) |
178 | local xp = stats:findFirstChild( "XP" ) |
179 | if killer ~ = player then |
180 | kills.Value = kills.Value + 1 |
181 | cash.Value = cash.Value + 50 |
182 | xp.Value = xp.Value + 3 |
185 | kills.Value = kills.Value - 0 |
188 | Send_DB_Event_Kill(killer, player) |
193 | game.Players.ChildAdded:connect(onPlayerEntered) |