Why LeaderStats folder doesn`t creates| Roblox Studio, Lua - lua

My leaderstats script doesnt work for some reason. I dont know why.
When i testing game in roblox studio folder just doesnt creating.
Because of the bug some information doesn`t loads.
There is the code (Script located in ServerScriptStorage):
wait(1)
print("Script LeaderStats started!")
local dataStoreService = game:GetService("DataStoreService")
local clicksDataStore = dataStoreService:GetDataStore("Clicks")
game.Players.PlayerAdded:Connect(function(Player)
local PStats = Instance.new("Folder", Player)
PStats.Name = "PStats"
local clicks = Instance.new("IntValue", PStats)
clicks.Name = "Clicks"
clicks.Value = 0
local playerUserId = "player_"..Player.UserId
-- loading data
local clicksData
local success, errormessage = pcall(function()
clicksData = clicksDataStore:GetAsync(playerUserId, clicksValue)
end)
if success then
clicks.Value = clicksData
end
end)
-- saving data
game.Players.PlayerRemoving:Connect(function(Player)
local playerUserId = "player_"..Player.UserId
local clicksValue = Player.PStats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end)
game:BindToClose(function(Player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..Player.UserId
local clicksValue = Player.PStats.Clicks.Value
local success, errormessage = pcall(function(Player)
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end
end)
I tried putting it in workspace, but folder didn`t create.

Make sure it's a Server Script inside of ServerScriptService.
replace line 31 with
clicksData = clicksDataStore:GetAsync(playerUserId)
If you want it to show as a leaderboard, replace line 15 with
PStats.Name = "leaderstats"

Related

Roblox Studio (Lua) - Loading Data (Not Working)

This is my datastore script, it has leaderstats incorporated. I an trying to save & load the Clicks and the Rebirths that the player accumulated. When I leave it told me that the data was saved but when I join there Isn't any data loaded
Can someone please help me, tell me what am i doing wrong?
I also have "Enable Studio Acces to API Services" set to enabled. This problem persists with every other script i use.
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("Data_2") -- Change the string value inbetween the "" to reset/change data stores
local LoadData = true -- Don't edit
local CreateData = true -- Don't edit
local function OnPlayerJoin(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = Leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = Leaderstats
local PlayerUserId = "Player_"..player.UserId
local data = PlayerData:GetAsync(PlayerUserId)
if data and LoadData == true then
warn("Loading "..PlayerUserId.." Data!")
Clicks.Value = data["Clicks"]
Rebirths.Value = data["Rebirths"]
print("Loaded "..PlayerUserId.." Data! 👍")
elseif CreateData == true then
warn("Creating "..PlayerUserId.." Data!")
Clicks.Value = 0
Rebirths.Value = 0
print("Created "..PlayerUserId.." Data! 👍")
else
warn("Check StoreData and CreateData, they may be off! ⚠️")
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)
print("Data Save Success!")
end)
if not success then
warn("Data Save Has Failed! ⚠️")
local PlayerUserId = "Player_"..player.UserId
PlayerData:SetAsync(PlayerUserId, player_stats)
warn("Retrying Data Save! ⚠️")
end
end
game.Players.PlayerAdded:Connect(OnPlayerJoin)
game.Players.PlayerRemoving:Connect(OnPlayerExit)

How do I remove a leaderstats value

I Cant get rid of Mana, I tried to delete the folder and when I went back in the game the folder was still there what should I do?...
CODE--
DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new('IntValue')
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Rebirths = Instance.new('IntValue')
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
-- Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Clicks.Value = data.Clicks
Rebirths.Value = data.Rebirths
-- Set our data equal to the current Clicks
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
Clicks = player.leaderstats.Clicks.Value;
Rebirths = player.leaderstats.Rebirths.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error!")
warn(errormessage)
end
end)
If you can't find the script that creates the leaderstat you could use script like the following to delete the extra leaderstats folder containing the Mana value. I would recommend this because the extra leaderstat has to be generated somewhere and it's best to just get rid of the script creating it.
Anyway you could try this: Add the following code to the ServerScriptService
game.Players.OnPlayerAdded:Connect(function(player)
wait(5) -- to wait for the script that adds the leaderstats to run
for _, child in ipairs(player:GetChildren()) do
if child.Name == "leaderstats" then
for _, value in ipairs(child:GetChildren()) do
if value.Name == "Mana" then
child:Destroy()
end
end
end
end
end)
What the above script does is it checks if a leaderstats folder inside the player exists with the value Mana, if it does, it will destroy the leaderstats folder.
The code posted above is not adding the extra leaderstats folder and value mana. User the search in the explorer to find any scripts in your game. Just type script into your search. Then open each file in your game hit Ctl + F on your keyboard and find Mana in each script.
Why this could happen? There are many assets from the toolbox that come bundled with scripts which could be one cause of your problem, but it can also happen because of plugins you have installed which add leaderstats to your game.

Roblox Studio : DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests

i am making a clicker game on roblox i am trying to save and load clicks that a player has
But i keep getting this warning (in the title) and i dont know how to fix it. I am trying for 2 days to fix it and i am just stuck
local DataStore = game:GetService("DataStoreService"):GetDataStore("Data")
function Load(player, Clicks)
local ClicksData
local succes, errormsg = pcall(function()
ClicksData = DataStore:GetAsync(player.UserId .. "Clicks")
end)
if not succes then
warn("Error loading" .. player.Name .. "'s data because: " .. errormsg)
end
if ClicksData then
Clicks.Value = ClicksData
end
end
function Save(player)
local succes, errormsg = pcall(function()
DataStore:SetAsync(player.UserId .. " Clicks", player.leaderstats.Clicks.Value)
end)
if not succes then
warn("Error saving " .. player.Name .. "'s data because: " .. errormsg)
end
end
game.Players.PlayerAdded:Connect(function(player)
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = player
local Clicks = Instance.new("NumberValue")
Clicks.Name = "Clicks"
Clicks.Value = 0
Clicks.Parent = ls
local Rebirths = Instance.new("NumberValue")
Rebirths.Name = "Rebirths"
Rebirths.Value = 0
Rebirths.Parent = ls
local Gems = Instance.new("NumberValue")
Gems.Name = "Gems"
Gems.Value = 0
Gems.Parent = ls
end)
game.Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
Save(plr)
end
end)
I tried on many other sites to fix this but i don't know how.
Can someone please help?
I saw on the devforum that i should use tables instead of values but i am a begginner and i do not know how to use them

leaderstats not working (Never thought they won't)

So, my leaderstats is not working. It should create a folder inside my player, but it doesn't.
Tried some things to fix, but nothing helped. (This intelligent Ai doesn't want me to post this again)
Here are all of my code in leaderstats:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ServerStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toolConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))
local dataStore = DataStoreService:GetDataStore("Test")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local rebirths = Instance.new("NumberValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local rebirths
local cash
rebirths = dataStore:GetAsync(player.UserId.."_Rebirths")
cash = dataStore:GetAsync(player.UserId.."_Cash")
if rebirths ~= nil then
player.leaderstats.Rebirths.Value = rebirths
end
if cash ~= nil then
player.leaderstats.Cash.Value = cash
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local succes, errormsg = pcall(function()
dataStore:SetAsync(player.UserId.."_Rebirths", player.leaderstats.Rebirths.Value)
dataStore:SetAsync(player.UserId.."_Cash", player.leaderstats.Cash.Value)
end)
if errormsg then
print ("Data error!")
end
end)
1: You forgot to assign serverstorage.
2. Based directly off the code, it seems like the code is on Roblox. You made the mistake of forgetting to check the developer console. That should help.

My data store system isn't working, thought I get a successfully saved message. Roblox

So I made a data store system that saves Silver. I used pcalls and whenever the player leaves I either get no message or just successfully saved, it's weird I never get any errors.
It doesn't work though. I tried doing it in Roblox itself, not just Studio. Does not work. This is a server script in ServerScriptService.
Please help :D
local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local silver = Instance.new("IntValue")
silver.Name = "Silver"
silver.Parent = leaderstats
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(playerUserId)
end)
if success then
silver.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserId = "Player_"..plr.UserId
local data = plr.leaderstats.Silver.Value
local success, errormessage = pcall(function()
dataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
end)```
Datastores require a string as a key. You are passing an integer to SetAsync, you will need to convert this to a string using the tostring() function.
Your corrected code should look like this.
local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local silver = Instance.new("IntValue")
silver.Name = "Silver"
silver.Parent = leaderstats
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(tostring(playerUserId))
end)
if success then
silver.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserId = "Player_"..plr.UserId
local data = plr.leaderstats.Silver.Value
local success, errormessage = pcall(function()
dataStore:SetAsync(tostring(playerUserId), data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
end)

Resources