I'm trying to make a script in wich gives a player a certain ammount of money based on what you typed in, But i get an error in wich i do not understand and i would like help.
Code:
function GiveMoney() local name = script.Parent:WaitForChild("Name") local money = script.Parent:WaitForChild("Money") game.Players.name.leaderstats.Money.Value = game.Players.name.leaderstats.Money.Value + money end script.Parent.Confirm.MouseButton1Click:connect(GiveMoney)
Error:
Players.bestrindberg.PlayerGui.Admin_GUI.GUI.Script:4: attempt to index field 'leaderstats' (a nil value)
Easy fix.
when you index like thing.name
, it's the same as thing["name"]
, you don't want a string however, you want a variable. Instead of game.Players.name
, do game.Players[name]
It seems to me that you don't currently have a leaderboard. Try using this.
First, let's create a Script in the Workspace and name it 'Stats'. Once we've done that, we can begin.
We'll begin with defining our leaderboard values. We'll use a nested array..
local boardStats = { { "IntValue", "Money" } }
Now that we have some values to play with, we'll need our function to usher our leaderboard into existence.
function LeaderBoard(player) local leaderstats = Instance.new("IntValue", player) leaderstats.Name = "leaderstats" end
This will make the leaderboard visible in the PlayerList. Now we need to add our leaderboard values. To do this, we will use a for loop.
function LeaderBoard(player) local leaderstats = Instance.new("IntValue", player) leaderstats.Name = "leaderstats" for n in pairs(boardStats) do local stat = Instance.new(boardStats[n][1], leaderstats) stat.Name = boardStats[n][2] end end
With this for loop, we're traversing the boardStats array, one nested array at a time. We come through the first time, and we'll find that boardStats[1] is equal to { "IntValue", "Money" } and we need to separate these. So we create an instance of boardStats[n][1] and place it into leaderstats. Once we've done that, we name the instance boardStats[n][2].
Last but not least, we need to connect the PlayerAdded function to get this to add values to every new player.
game:GetService("Players").PlayerAdded:connect(LeaderBoard)
Once that's done, altogether we have our leaderboard!
local boardStats = { { "IntValue", "Money" } } function LeaderBoard(player) local leaderstats = Instance.new("IntValue", player) leaderstats.Name = "leaderstats" for n in pairs(boardStats) do local stat = Instance.new(boardStats[n][1], leaderstats) stat.Name = boardStats[n][2] end end game:GetService("Players").PlayerAdded:connect(LeaderBoard)
Your button should then be able to function.