I have a buy rank gui with a yes button among others but when i click yes it says 14:43:34.297 - ServerScriptService.Leaderstats:28: attempt to index upvalue 'stats' (a nil value)
14:43:34.297 - Stack Begin
14:43:34.298 - Script 'ServerScriptService.Leaderstats', Line 28
14:43:34.298 - Stack End idk what this means, someone pls help yes local script:
local rep = game:GetService("ReplicatedStorage") script.Parent.MouseButton1Click:Connect(function() script.Parent.Visible = false script.Parent.Parent.no.Visible = false script.Parent.Parent.Rank.Visible = true --if game:GetService("Players").LocalPlayer.leaderstats.Money.Value >= 50000 and game:GetService("Players").LocalPlayer.Rank.Value == 0 then--i cut this part out because i didnt wanna have to have the money while testing rep:WaitForChild("Rank"):FireServer() --end--cut this part out to not have an end error since i cut out the if statement end)
game.Players.PlayerAdded:Connect(function(player) local stats = player:FindFirstChild("leaderstats") local Key = "Player-ID:" .. player.UserId local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local Money = Instance.new("NumberValue", leaderstats) Money.Name = "Money" game:GetService("ReplicatedStorage"):WaitForChild("Money").OnServerEvent:Connect(function() stats.Money.Value = stats.Money.Value + game.Workspace.Base.Seller.seller.ironore1.Value.Value end) local Rank = Instance.new("NumberValue", leaderstats) Rank.Name = "Rank" game:GetService("ReplicatedStorage"):WaitForChild("Rank").OnServerEvent:Connect(function() stats.Rank.Value = stats.Rank.Value + 1 end)
this is not the entire script but its enough to understand
You are finding the stats before it is even created causing it to be 'nil'. Your code is also pretty messy, I suggest you invest time in learning how events work.
My approach would be to create the leaderstats and loading then data in in a PlayerAdded event. Then you would create a RemoteEvent that would rank the person up.
game.Players.PlayerAdded:Connect(function(player) local stats = Instance.new('Folder', player) stats.Name = 'leaderstats' local Rank = Instance.new('IntValue', stats) Rank.Name = 'Rank' end) local rankUp = Instance.new('RemoteEvent', ReplicatedStorage) rankUp.Name = 'rankUp' rankUp.OnServerEvent:Connect(function(player) local Rank = player.leaderstats:FindFirstChild('Rank') Rank.Value = Rank.Value + 1 end)
We are creating an event that triggers every time a player is added. When it's added, we are creating a leaderstat folder for it as well as creating a new stat called 'Rank'. This would also be a good place for loading data since you only need to load data upon entering the game. We are then creating a remote event that upon triggering will look for the stat called 'Rank' (the one we created) and changing its value accordingly.
Note: This remote event would be labled as 'exploitable'. Make sure to always do server checks to make it secure.
local rankUp = ReplicatedStorage:WaitForChild('rankUp') rankUp:FireServer()
Now that that is done, all the client has to do is trigger the event by :FireServer().