The program shouldd be spawning blocks of random colour above the players head, while the blocks spawn, a leaderboard will count the amount of blocks. For each new player joining into the game, the counter will create a new line with reseted numbers. The problem is that the blocks are not spawning above the head. This is the code:
-- This script creates a leaderboard that displays the amount of blocks a player has spawned
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Create a new folder to store the leaderboard values
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = game
-- Create a function that adds a new value to the leaderboard for a given player
local function addLeaderboardValue(player)
-- Create a new int value
local value = Instance.new("IntValue")
-- Set the value's name and initial value to 0
value.Name = "Blocks Spawned"
value.Value = 0
-- Parent the value to the player's leaderstats folder
value.Parent = player:FindFirstChild("leaderstats")
end
-- Loop through all the players in the game
for _, player in ipairs(Players:GetPlayers()) do
-- Create a new folder for each player's leaderstats
local stats = leaderstats:Clone()
stats.Parent = player
-- Add a new value to the leaderboard for each player
addLeaderboardValue(player)
end
-- Listen for new players joining the game
Players.PlayerAdded:Connect(function(player)
-- Create a new folder for the new player's leaderstats
local stats = leaderstats:Clone()
stats.Parent = player
-- Add a new value to the leaderboard for the new player
addLeaderboardValue(player)
end)
-- Modify the existing script that spawns blocks to increment the leaderboard value for each player
local function spawnBlock(player)
-- Create a new part
local part = Instance.new("Part")
-- Set the part's size and color
part.Size = Vector3.new(2, 2, 2)
part.Color = Color3.new(math.random(), math.random(), math.random())
-- Set the part's position above the player's head
local head = player.Character and player.Character:FindFirstChild("Head")
if head then
part.Position = head.Position + Vector3.new(0, 5, 0)
end
-- Parent the part to the workspace
part.Parent = workspace
-- Increment the player's leaderboard value by 1
local value = player:FindFirstChild("leaderstats"):FindFirstChild("Blocks Spawned")
if value then
value.Value = value.Value + 1
end
end
-- Loop through all the players in the game
for _, player in ipairs(Players:GetPlayers()) do
-- Spawn a block for each player every 2 seconds
spawnBlock(player)
RunService.Heartbeat:Connect(function()
wait(2)
spawnBlock(player)
end)
end
-- Listen for new players joining the game
Players.PlayerAdded:Connect(function(player)
-- Spawn a block for the new player every 2 seconds
spawnBlock(player)
RunService.Heartbeat:Connect(function()
wait(2)
spawnBlock(player)
end)
end)
The first problem is that you are not creating the leaderstats folder inside of the player, only attempting to change it.
Also, you are listening to the heartbeat event, and then waiting. This means that you will get a block spawned every frame, but just delayed by two seconds. You should use a while loop instead of listening to heartbeat.
-- This script creates a leaderboard that displays the amount of blocks a player has spawned
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Create a new folder to store the leaderboard values
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = game
-- Create a function that adds a new value to the leaderboard for a given player
local function addLeaderboardValue(player)
-- Create a new int value
local value = Instance.new("IntValue")
-- Set the value's name and initial value to 0
value.Name = "Blocks Spawned"
value.Value = 0
-- Parent the value to the player's leaderstats folder
value.Parent = player:FindFirstChild("leaderstats")
end
-- Loop through all the players in the game
for _, player in ipairs(Players:GetPlayers()) do
-- Create a new folder for each player's leaderstats
local stats = leaderstats:Clone()
stats.Parent = player
-- Add a new value to the leaderboard for each player
addLeaderboardValue(player)
end
-- Listen for new players joining the game
Players.PlayerAdded:Connect(function(player)
-- Create a new folder for the new player's leaderstats
local stats = leaderstats:Clone()
stats.Parent = player
-- Add a new value to the leaderboard for the new player
addLeaderboardValue(player)
end)
-- Modify the existing script that spawns blocks to increment the leaderboard value for each player
local function spawnBlock(player)
-- Create a new part
local part = Instance.new("Part")
-- Set the part's size and color
part.Size = Vector3.new(2, 2, 2)
part.Color = Color3.new(math.random(), math.random(), math.random())
-- Set the part's position above the player's head
local head = player.Character and player.Character:FindFirstChild("Head")
if head then
part.Position = head.Position + Vector3.new(0, 5, 0)
end
-- Parent the part to the workspace
part.Parent = workspace
-- Increment the player's leaderboard value by 1
local value = player:FindFirstChild("leaderstats"):FindFirstChild("Blocks Spawned")
if value then
value.Value = value.Value + 1
end
end
-- Loop through all the players in the game
for _, player in ipairs(Players:GetPlayers()) do
-- Spawn a block for each player every 2 seconds
spawnBlock(player)
-- Prevent this from blocking the thread.
task.spawn(function()
while true do
wait(2)
spawnBlock(player)
end
end)
end
-- Listen for new players joining the game
Players.PlayerAdded:Connect(function(player)
if not player:FindFirstChild("leaderstats") then
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
-- Spawn a block for the new player every 2 seconds
spawnBlock(player)
while true do
wait(2)
spawnBlock(player)
end
end)