leader board not working in roblox studio - lua

I am trying to make a leader board that tells the player how much cats they have collected. the player has a file and the script counts how many items the file has then displays it on the leaderboard, but it doesn't work(shows the leaderboard but no second row for the amount collected)! Any ideas??
here is my code:
`local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder", player)
folder.Name = "leaderstats"
local catsamount = Instance.new("IntValue", folder)
catsamount.Name = "Cats Found"
catsamount.Value = 0
while wait(0.1) do
catsamount.Value = 0
for _, v in pairs(catscollected:GetChildren()) do
catsamount.Value = catsamount.Value + 1
end
end
end)`

You have to parent catsamount to folder and folder to player:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local catsamount = Instance.new("IntValue", folder)
catsamount.Name = "Cats Found"
catsamount.Value = 0
catsamount.Parent = folder
while wait(0.1) do
catsamount.Value = 0
for _, v in pairs(catscollected:GetChildren()) do
catsamount.Value = catsamount.Value + 1
end
end
end)
This is so that the folder will be added to the player after it is created - Instance.new() creates an instance, but it does not parent the created instance to anything so it will not appear anywhere.

Related

Roblox script, doesn't work but doesn't show any errors

I'm making a baldi basics fan game in Roblox, and once you pick up the notebook, it checks when if the player has 7 coins (notebooks, placeholder name) and it doesn't work, it doesn't seem to show any errors in the script editor.
I tried this script:
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1
script.Parent.Sound:Play()
script.Parent:Remove()
game.ServerStorage.artem.Parent = workspace
game.Workspace.lobby:Stop()
game.Workspace.artem_passive.Parent = game.ReplicatedStorage
if plr.leaderstats.Coins.Value >= 7 then -- this is the part that doesn't work
game.Workspace["ready up"]:Play()
game.Workspace["invisible wall"].CanCollide = false
end
end)
There's also a leaderboard script i added in with, that is working.
game.Players.PlayerAdded:connect(function(plr)
local f = Instance.new("Folder", plr)
f.Name = "leaderstats"
local coins = Instance.new("IntValue", f)
coins.Name = "Coins"
coins.Value = 0
end)

How to fix this?? Roblox Studio

Sup guys, im working on level system in roblox studio. So i find a funny error when i spent all stats points and level up me added not 4 points but so many points like i didnt spend anything
Here the script:
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local lvl = Instance.new("NumberValue", leaderstats)
lvl.Name = "Level"
lvl.Value = 1
local exp = Instance.new("NumberValue", leaderstats)
exp.Name = "Exp"
exp.Value = 0
local reqexp = Instance.new("NumberValue", Player)
reqexp.Name = "reqexp"
reqexp.Value = lvl.Value * 100
local hp = Instance.new("NumberValue", Player)
hp.Name = "Health"
hp.Value = 100
local points = Instance.new("NumberValue", Player)
points.Name = "Points"
points.Value = 0
exp.Changed:Connect(function(Changed)
if exp.Value >= reqexp.Value then
points.Value += 4
exp.Value = 0
lvl.Value += 1
reqexp.Value = lvl.Value * 100
end
end)
end)
I tried many ways but nothing help

attempt to index nil with "clicks" fix?

here it is, it is roblox code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local player = game:GetService("Players")
local leaderstats = player:WaitForChild('leaderstats', 10)
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local function clicksStore()
clicks.Value = clicks.Value + 1
if rebirths <= 1 then
clicks.Value = clicks.Value + rebirths.Value
end
end
remoteEvent.OnServerEvent:Connect(clicksStore)
is there anyway i can fix this? its getting on my nerves. Thank you for helping!
You are indexing the leaderstats correctly, but in a server Script, you aren't accessing the player correctly.
You can easily access the player by using the RemoteEvent's OnServerEvent connection.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local function clicksStore(player)
local leaderstats = player.leaderstats
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local clickValue = 1 + rebirths.Value
clicks.Value += clickValue
end
remoteEvent.OnServerEvent:Connect(clicksStore)

Roblox autosave leadboard but save only one Value

I tried to make the functions via Datastore so that it only saved the data Wins and reset the stage always but saved Wins always so if I have 250 stages and 1 win and I leave the game I will have 0 stages and 1 win.
local CheckpointService = {}
-- SERVICES
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
-- MODULES
local AchievementService = require(game.ServerScriptService.Server:WaitForChild("Achievement"))
-- VARIABLES
local CHECKPOINTS = game.Workspace:WaitForChild("Checkpoints")
local STAT_NAME = "Stage"
local WINS_NAME = "Wins"
local PREVENT_SKIPPING = false
-- CONNECTIONS
local function playerAdded(player)
local leaderstats = Instance.new("Folder", player) or player:FindFirstChild("leaderstats")
leaderstats.Name = "leaderstats"
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = STAT_NAME
checkpointStat.Value = 1
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = WINS_NAME
checkpointStat.Value = 0
player.CharacterAdded:connect(function(character)
repeat
wait()
until CHECKPOINTS and checkpointStat
local goto = CHECKPOINTS:FindFirstChild("Checkpoint " .. checkpointStat.Value)
if goto then
character:MoveTo(goto.Position)
else
warn("[SERVER] - Checkpoint " .. checkpointStat.Value .. " not found!")
end
end)
end

How to score when killing an NPC

When I kill an NPC, my score should increase, but it does not.
Here's my code. It is located under my NPC model.
local Humanoid = script.Parent.Humanoid
function PwntX_X()
local tag = Humanoid:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild("leaderstats")
if Leaderstats ~= nil then
Leaderstats.Score.Value = Leaderstats.Score.Value + 250 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)
And here's my code for the leaderboard
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue",plr)
stats.Name = "leaderstats"
local score = Instance.new("IntValue", stats)
score.Name = "Score"
score.Value = 0
end)
Not sure, but looks like
score.Value = Leaderstats.Score.Value

Resources