Leaderstats not saving, help me please?
Hello! I have an RPG in pre alpha. Recently, the levels and other stats stopped saving.
Here's the script:
001 | function onXPChanged(player, XP, level) |
002 | if XP.Value> = level.Value * 50 then |
003 | XP.Value = XP.Value - level.Value * 50 |
004 | level.Value = level.Value + 1 |
005 | player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 50 |
006 | player.Character.Humanoid.Health = player.Character.Humanoid.Health + 50 |
010 | function onLevelUp(player, XP, level) |
011 | if player.Character~ = nil then |
013 | local fireworks = Instance.new( "Part" ) |
015 | fireworks.formFactor = "Symmetric" |
016 | fireworks.Size = Vector 3. new( 1 , 1 , 1 ) |
017 | fireworks.BrickColor = BrickColor.Random() |
018 | fireworks.CFrame = player.Character.Head.CFrame + Vector 3. new( 0 , 2 , 0 ) |
019 | fireworks.Parent = game.Workspace |
020 | game:GetService( "Debris" ):AddItem(fireworks, 2 ) |
021 | fireworks.Velocity = Vector 3. new(math.random(- 30 , 30 ),math.random(- 30 , 30 ),math.random(- 30 , 30 )) |
024 | local m = Instance.new( "Hint" ) |
025 | m.Parent = game.Workspace |
026 | m.Text = player.Name .. " has leveled up!" |
031 | function saveScore 4 (player, score) |
032 | player:SaveNumber( "Stardust" , score) |
035 | function saveScore(player, score) |
036 | player:SaveNumber( "Level" , score) |
039 | function saveScore 2 (player, score) |
040 | player:SaveNumber( "XP" , score) |
043 | function saveScore 3 (player, score) |
044 | player:SaveNumber( "Gold" , score) |
048 | function loadScore(player, clickCounter) |
049 | local score = player:LoadNumber( "Level" ) |
052 | clickCounter.Value = score |
054 | print ( "Nothing to load/score was 0" ) |
059 | function loadScore 2 (player, clickCounter) |
060 | local score = player:LoadNumber( "XP" ) |
063 | clickCounter.Value = score |
065 | print ( "Nothing to load/score was 0" ) |
070 | function loadScore 3 (player, clickCounter) |
071 | local score = player:LoadNumber( "Gold" ) |
074 | clickCounter.Value = score |
076 | print ( "Nothing to load/score was 0" ) |
081 | function loadScore 4 (player, clickCounter) |
082 | local score = player:LoadNumber( "Stardust" ) |
085 | clickCounter.Value = score |
087 | print ( "Nothing to load/score was 0" ) |
092 | function onPlayerEntered(newPlayer) |
096 | local stats = Instance.new( "IntValue" ) |
097 | stats.Name = "leaderstats" |
099 | local clicks = Instance.new( "IntValue" ) |
100 | clicks.Name = "Level" |
103 | local clicks 2 = Instance.new( "IntValue" ) |
107 | local clicks 3 = Instance.new( "IntValue" ) |
108 | clicks 3. Name = "Gold" |
111 | local clicks 4 = Instance.new( "IntValue" ) |
112 | clicks 4. Name = "Stardust" |
116 | clicks.Parent = stats |
117 | clicks 2. Parent = stats |
118 | clicks 3. Parent = stats |
119 | clicks 4. Parent = stats |
120 | stats.Parent = newPlayer |
122 | clicks 2. Changed:connect( function () onXPChanged(newPlayer, clicks 2 , clicks) end ) |
123 | clicks.Changed:connect( function () onLevelUp(newPlayer, clicks 2 , clicks) end ) |
125 | newPlayer.WaitForDataReady = true |
128 | loadScore(newPlayer, clicks) |
129 | loadScore 2 (newPlayer, clicks 2 ) |
130 | loadScore 3 (newPlayer, clicks 3 ) |
131 | loadScore 4 (newPlayer, clicks 4 ) |
134 | newPlayer.CharacterAdded:connect( function (c) |
135 | c.Humanoid.MaxHealth = newPlayer.leaderstats.Level.Value * 50 + 50 |
136 | c.Humanoid.Health = newPlayer.leaderstats.Level.Value * 50 + 50 |
140 | function onPlayerRemoving(player) |
141 | print ( "Attempting to save score for " .. player.Name) |
142 | local stats = player:FindFirstChild( "leaderstats" ) |
143 | if (stats ~ = nil ) then |
144 | local clicks = stats:FindFirstChild( "Level" ) |
145 | local clicks 2 = stats:FindFirstChild( "XP" ) |
146 | local clicks 3 = stats:FindFirstChild( "Gold" ) |
147 | local clicks 3 = stats:FindFirstChild( "Stardust" ) |
148 | if (clicks ~ = nil ) and (clicks 2 ~ = nil ) and (clicks 3 ~ = nil ) and (clicks 4 ~ = nil ) then |
149 | saveScore(player, clicks.Value) |
150 | saveScore 2 (player, clicks 2. Value) |
151 | saveScore 3 (player, clicks 3. Value) |
152 | saveScore 4 (player, clicks 4. Value) |
157 | game.Players.PlayerAdded:connect(onPlayerEntered) |
158 | game.Players.PlayerRemoving:connect(onPlayerRemoving) |
Any fixes for this error?