Roblox autosave leadboard but save only one Value - lua

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

Related

leader board not working in roblox studio

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.

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 create a DataStore?

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')
-- Erstellt Leaderboard
local function onPlayerJoin(player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local clicks = Instance.new('IntValue')
clicks.Name = 'Clicks'
clicks.Value = 0
clicks.Parent = leaderstats
local rebirths = Instance.new('IntValue')
rebirths.Name = 'Rebirths'
rebirths.Value = 0
rebirths.Parent = leaderstats
-- Wenn ein Nutzer verlassen hat das die daten gespeichert wurden
local playerUserId = 'Player_'..player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
clicks.Value = data['Clicks']
rebirths.Value = data['Rebirths']
else -- Wenn user das erste mal joint das value 0 ist
clicks.Value = 0
rebirths.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function ()
local playerUserId = 'Player_'..player.UserId
playerData:SetAsync(playerUserId, player_stats)
end)
if not success then
warn('Could not save data')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
I tried to make a DataStore that stores the values of the leaderboard into a data that returns everytime if i rejoin. But it doesnt work and idk why. Im kinda new into lua programming but u guys can help me :D thanks.

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)

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