What do you want to achieve? I want to make a global leaderboard. I used this script:
local DataStoreService = game:GetService("DataStoreService")
local KillsLeaderboard = DataStoreService:GetOrderedDataStore("KillsLeaderboard")
local function updateLeaderboard()
local success, errorMessage = pcall(function()
local Data = KillsLeaderboard:GetSortedAsync(false, 5)
local KillsPage = Data:GetCurrentPage()
for rank, data in ipairs (KillsPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Kills = data.value
local isOnLeaderboard = false
for i, v in pairs(game.Workspace.InfoSign.Leaderboard.LeaderboardGui.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Kills and IsOnLeaderboard == false then
local NewLeaderboardFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
NewLeaderboardFrame.Player.Text = Name
NewLeaderboardFrame.Kills.Text = Kills
NewLeaderboardFrame.Rank.Text = "#"..Rank
NewLeaderboardFrame.Position = Udim2.new(0, 0, NewLeaderboardFrame.Position.Y.Scale + (.08 * #game.Workspace.InfoSign.LeaderboardGui.SurfaceGui.Holder:GetChildren()), 0)
NewLeaderboardFrame.Parent = game.Workspace.InfoSign.Leaderboard.LeaderboardGui.Holder
end
end
end)
if not success then
print(errorMessage)
end
end
while true do
for_, player = pairs(game.Players:GetPlayers()) do
KillsLeaderboard:SetAsync(player.UserId, player.leaderstats.kills.Value)
end
for _, frame in pairs(game.Workspace.InfoSign.Leaderboard.LeaderboardGui.Holder:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
print ("Updated!")
wait (60)
end
What is the issue? it keeps showing me this error:
ServerScriptService.LeaderboardHandler:39: attempt to index nil with âkillsâ
What solutions have you tried so far? I thought that I maybe capitalized it wrong, but this was not the case. I thought that I maybe broke the parenting stuff but thatâs not the case. I thought i used a wrong type of a local variable but nah. Help me?
By the way, this is the parenting:
and hereâs the leaderstats script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = leaderstats
end)
Can you go into studio, then click join game, and go to explorer â players â yourplayer â leaderstats and click on the kills value and see if it is there?
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = leaderstats
end)
for _, player in ipairs(game.Players:GetPlayers() do
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = leaderstats
end
This way, youâll be sure that leaderstats is created and parented to new players and to current players.