Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Why doesnt my rank go up when i click yes?

Asked by 3 months ago

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)

leaderboard script:

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

1
I can't read this BlackOrange3343 2397 — 3mo
0
what? Gameplayer365247v2 551 — 3mo
0
I think he meant that he can't understand it CjayPlyz 442 — 3mo
0
Put it in Lua form Creacoz 107 — 3mo
View all comments (3 more)
0
? this is lua form Gameplayer365247v2 551 — 3mo
0
Idk if this is relevant, but you're trying to access leaderstats before you actually create the folder. msuperson24 47 — 3mo
0
no the folder is created before, the leaderstat works i just cant manage to get the numbers in there to go up every time Gameplayer365247v2 551 — 3mo

1 answer

Log in to vote
0
Answered by 3 months ago

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.

Server Script:

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.

Client Script:

local rankUp = ReplicatedStorage:WaitForChild('rankUp')
rankUp:FireServer()

Now that that is done, all the client has to do is trigger the event by :FireServer().

Ad

Answer this question