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.
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)
I was making a game, with a status system that includes some points to give away. I used roblox's DataStore service, but I'm having a problem with my game. After a player spends his points, distributing them in the stats he wants, after he leaves the game and re-enters those same points back to him with the same value, this can cause a point doubling glitch, which makes the unfair game, which I don't want. I think there is an error in my code, what can I do to fix it?
Code below:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlrData")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(Plr)
local data = Instance.new("Folder", Plr)
data.Name = "Data"
local exp = Instance.new("IntValue", data)
exp.Name = "Exp"
exp.Value = 1
local levels = Instance.new("IntValue", data)
levels.Name = "Level"
levels.Value = 1
local Points = Instance.new("IntValue", data)
Points.Name = "Points"
Points.Value = 3
local expneed = Instance.new("IntValue", data)
expneed.Name = "ExpNeeded"
expneed.Value = 100
local strenght = Instance.new("IntValue", data)
strenght.Value = 1
strenght.Name = "Strenght"
local Health = Instance.new("IntValue", data)
Health.Value = 100
Health.Name = "Health"
local char = Plr.Character or Plr.CharacterAdded:Wait()
local charhealth = char.Humanoid.Health
local DF = Instance.new("IntValue", data)
DF.Value = 1
DF.Name = "DF"
local PlayerId = Plr.UserId
local PlayerData = DataStore:GetAsync(PlayerId)
if PlayerData then
strenght.Value = PlayerData["Strenght"]
Health.Value = PlayerData["Health"]
DF.Value = PlayerData["DF"]
exp.Value = PlayerData["Exp"]
levels.Value = PlayerData["Level"]
expneed.Value = PlayerData["ExpNeeded"]
Points.Value = PlayerData["Points"]
charhealth = PlayerData["Health"]
end
end)
local function tab(Player)
local PlrStats = {}
for _, v in pairs(Player.Data:GetChildren()) do
PlrStats[v.Name] = v.Value
end
return PlrStats
end
game.Players.PlayerRemoving:Connect(function(Plr)
local PlrStats = tab(Plr)
local Sucess, Result = pcall(function()
local PlrId = Plr.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error save")
end
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
task.wait()
local PlrStats = tab(Player)
local Sucess, Result = pcall(function()
local PlrId = Player.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error")
end
end
end)
game.Players.PlayerAdded:Connect(function(Player)
task.wait(0.1)
local Level = Player.Data.Level
local Exp = Player.Data.Exp
local ExpNeed = Player.Data.ExpNeeded
local Points = Player.Data.Points
local Strenght = Player.Data.Strenght
local Health = Player.Data.Health
local DF = Player.Data:WaitForChild("DF")
RunService.Heartbeat:Connect(function()
if Exp.Value == 0 and ExpNeed.Value == 0 then
Exp.Value = 1
ExpNeed.Value = 100
end
if Points.Value > 300 then
Points.Value = 300
end
if Exp.Value >= ExpNeed.Value then
Exp.Value = Exp.Value - ExpNeed.Value
Level.Value += 1
ExpNeed.Value *= 1.13
Points.Value += 3
end
if Strenght.Value == 0 or DF.Value == 0 or Health.Value == 0 then
Strenght.Value = 1
DF.Value = 1
Health.Value = 1
end
if Strenght.Value > 100 or DF.Value > 100 then
Strenght.Value = 100
DF.Value = 100
end
if Health.Value > 1000 then
Health.Value = 1000
end
end)
RunService.Heartbeat:Connect(function()
if Level.Value >= 100 then
Level.Value = 100
end
end)
end)
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
Every value saves besides of StrMulti and probably EndMulti, ChakraMulti.
It just doesn't save at all. :c
(.............................................................................................)
(I need letters to post this. Ai doesn't want me to post this)
(Still can't post. Sorry for this extra text)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ServerStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local dataStore1 = DataStoreService:GetDataStore("Test")
local function waitForRequestBudget(requestType)
local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget < 1 do
currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
task.wait(5)
end
end
local function setupPlayerData(player: player)
local userID = player.UserId
local key = "Player_"..userID
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Multi = Instance.new("NumberValue", player)
Multi.Name = "Multi"
Multi.Value = 1
local rep = Instance.new("NumberValue", player)
rep.Name = "rep"
rep.Value = 0
local agZone = Instance.new("IntValue", player)
agZone.Name = "agZone"
agZone.Value = 1
local jfZone = Instance.new("IntValue", player)
jfZone.Name = "jfZone"
jfZone.Value = 1
local eMulti = Instance.new("NumberValue", player)
eMulti.Name = "eMulti"
eMulti.Value = 1
local Title = Instance.new("StringValue", leaderstats)
Title.Name = "Title"
Title.Value = "Innocent"
local eMultic = Instance.new("NumberValue", player)
eMultic.Name = "eMultic"
eMultic.Value = 1
local Agility = Instance.new("IntValue", player)
Agility.Name = "Agility"
Agility.Value = 90
local JumpForce = Instance.new("IntValue", player)
JumpForce.Name = "JumpForce"
JumpForce.Value = 0
local AgMulti = Instance.new("IntValue", player)
AgMulti.Name = "AgMulti"
AgMulti.Value = 1
local jfMulti = Instance.new("IntValue", player)
jfMulti.Name = "jfMulti"
jfMulti.Value = 1
local Chakra = Instance.new("IntValue", player)
Chakra.Name = "Chakra"
Chakra.Value = 0
local ZoneMulti = Instance.new("NumberValue", player)
ZoneMulti.Name = "ZoneMulti"
ZoneMulti.Value = 1
local eZoneMulti = Instance.new("NumberValue", player)
eZoneMulti.Name = "eZoneMulti"
eZoneMulti.Value = 1
local Strength = Instance.new("IntValue", player)
Strength.Name = "Strength"
Strength.Value = 100
local endZone = Instance.new("IntValue", player)
endZone.Name = "endZone"
endZone.Value = 1
local strZone = Instance.new("IntValue", player)
strZone.Name = "strZone"
strZone.Value = 1
local ChakraZone = Instance.new("IntValue", player)
ChakraZone.Name = "ChakraZone"
ChakraZone.Value = 1
local StrMulti = Instance.new("IntValue", player)
StrMulti.Name = "StrMulti"
StrMulti.Value = 1
local EndMulti = Instance.new("IntValue", player)
EndMulti.Name = "EndMulti"
EndMulti.Value = 1
local ChakraMulti = Instance.new("IntValue", player)
ChakraMulti.Name = "ChakraMulti"
ChakraMulti.Value = 1
local Endurance = Instance.new("IntValue", player)
Endurance.Name = "Endurance"
Endurance.Value = 0
local Yen = Instance.new("IntValue", player)
Yen.Name = "Yen"
Yen.Value = 0
local powers = Instance.new("Folder", player)
powers.Name = "Powers"
local hasAura = Instance.new("BoolValue", powers)
hasAura.Name = "hasAura"
hasAura.Value = true
local str4Q = Instance.new("NumberValue", player)
str4Q.Name = "Str4Q"
str4Q.Value = 0
local quest = Instance.new("IntValue", player)
quest.Name = "QuestNum"
quest.Value = 1
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore1.GetAsync,dataStore1, key)
until success or not Players:FindFirstChild(player.Name)
success, returnValue = pcall(dataStore1.GetAsync,dataStore1, key)
if success then
if returnValue == nil then
returnValue = {
Endurance = 0,
Strength = 0,
Chakra = 0,
Yen = 0,
Agility = 0,
JumpForce = 0,
rep = 0,
ChakraMulti = 0,
StrMulti = 0,
EndMulti = 0
}
end
Strength.Value = if returnValue.Strength ~= nil then returnValue.Strength else 0
Yen.Value = if returnValue.Yen ~= nil then returnValue.Yen else 0
Endurance.Value = if returnValue.Endurance ~= nil then returnValue.Endurance else 0
Chakra.Value = if returnValue.Chakra ~= nil then returnValue.Chakra else 0
Agility.Value = if returnValue.Agility ~= nil then returnValue.Agility else 0
JumpForce.Value = if returnValue.JumpForce ~= nil then returnValue.JumpForce else 0
rep.Value = if returnValue.rep ~= nil then returnValue.rep else 0
ChakraMulti.Value = if returnValue.ChakraMulti ~= nil then returnValue.ChakraMulti else 0
EndMulti.Value = if returnValue.EndMulti ~= nil then returnValue.EndMulti else 0
StrMulti.Value = if returnValue.StrMulti ~= nil then returnValue.StrMulti else 0
else
player:Kick("There was an error loading your Data. Roblox's DataStore might be down, try again later, or contact us!")
print(player.Name.."Data loading Error!")
end
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local strength = player.Strength.Value
local yen = player.Yen.Value
local endurance = player.Endurance.Value
local chakra = player.Chakra.Value
local agility = player.Agility.Value
local jumpforce = player.JumpForce.Value
local Rep = player.rep.Value
local strmulti = player.StrMulti.Value
local endmulti = player.EndMulti.Value
local chakramulti = player.ChakraMulti.Value
local dataTable = {
Strength = strength,
Yen = yen,
Endurance = endurance,
Chakra = chakra,
Agility = agility,
JumpForce = jumpforce,
rep = Rep,
ChakraMulti = chakramulti,
EndMulti = endmulti,
StrMulti = strmulti
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore1.UpdateAsync, dataStore1, key, function()
return dataTable
end)
until success
if success then
print("Data Saved!")
else
print("Data saving Error!")
end
end
local function onShutDown()
if RunService:IsStudio() then
task.wait(2)
else
local finished = Instance.new("BindableEvent")
local allPlayers = Players:GetPlayers()
local leftPlayers = #allPlayers
for _, player in ipairs(allPlayers) do
coroutine.wrap(function()
save(player)
leftPlayers -=1
if leftPlayers == 0 then
finished:Fire()
end
end)
end
finished.Event:Wait()
end
end
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(setupPlayerData)(player)
end
Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutDown)
while true do
task.wait(300)
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end