I am trying to make a leaderboard where players get 5 points each hour. Here are the scripts:
This one sets up the leaderboard.
local leaderboardSetup = {}
local Players = game:GetService("Players")
function leaderboardSetup.leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
end
Players.PlayerAdded:Connect(leaderboardSetup)
return leaderboardSetup
This one is a module script for adding points.
local addPoints = {}
local ServerStorage = game:GetService("ServerStorage")
local leaderboardSetup = require(ServerStorage:WaitForChild("LeaderboardSetup"))
leaderboardSetup.leaderboardSetup()
local player = game:GetService("Players")
local leaderstats = player.leaderstats
local points = leaderstats and leaderstats:FindFirstChild("Points")
function addPoints.addPoints()
points.Value = points.Value + 1
end
return addPoints
This is the one that adds the points each hour.
local ServerStorage = game:GetService("ServerStorage")
local addPoints = require(ServerStorage:WaitForChild("AddPoints"))
while true do
print("yes")
wait(10)
for currentValue = 1, 5 do
print("yeeee")
addPoints.addPoints()
end
end
It is saying that leaderstats isn’t a valid member of player in the second script. Please help! I am very new to coding, so I have no idea how to fix this!
This is because you’re calling the player service. Do this instead
Module:
local addPoints = {}
local ServerStorage = game:GetService("ServerStorage")
local leaderboardSetup = require(ServerStorage:WaitForChild("LeaderboardSetup"))
leaderboardSetup.leaderboardSetup()
function addPoints.addPoints(player)
local leaderstats = player.leaderstats
local points = leaderstats and leaderstats:FindFirstChild("Points")
points.Value = points.Value + 1
end
return addPoints
Script that adds the points
local ServerStorage = game:GetService("ServerStorage")
local addPoints = require(ServerStorage:WaitForChild("AddPoints"))
while true do
print("yes")
wait(10)
for currentValue = 1, 5 do
for i, Plr in pairs(game.Players:GetPlayers()) do
print("yeeee")
addPoints.addPoints(Plr)
end
end
end
As someone who has worked on multiple modular code, this is just not the way to go. This is an issue with the leaderboard setup script, as when you call the leaderstats to be added, then .PlayerAdded event will start, you also specify the table module instead of the function. It’ll be better to just start from scratch,
Make this a normal script(one use things like this really dont need to be module), the new leaderboard ssetup
local Players = game:GetService("Players")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
end
Players.PlayerAdded:Connect(leaderboardSetup)
you can also make the points adding module to this
local addPoints = {}
local ServerStorage = game:GetService("ServerStorage")
function addPoints.addPoints(player)
local leaderstats = player.leaderstats
local points = leaderstats and leaderstats:FindFirstChild("Points")
points.Value = points.Value + 1
end
return addPoints
basically, Game.players:GetPlayers() will return(or give) a table of all the players currently in the game. The for i, plr in pairs is to loop through each player we got from the :getPlayers() function. It’ll then add the points to the plr.