How do I make a save system for a leader board with more than 3 stats - scripting

I want to make a save system so that people don't have to restart every single time they play
I don't really know what to do so I will show you the code for my leader stats this is located in the work space
local function onPlayerJoin(player)
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "JumpBoost"
gold.Value = 150
gold.Parent = leaderstats
local speed = Instance.new("IntValue")
speed.Name = "Speed"
speed.Value = 20
speed.Parent = leaderstats
local coin = Instance.new("IntValue")
coin.Name = "CloudCoins"
coin.Value = 0
coin.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Value = 0
rebirths.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
Again I don't really know what to do so, please help.

The documentation for Data Stores is pretty good. An important warning for testing :
DataStoreService cannot be used in Studio if a game is not configured to allow access to API services.
So you will have to publish the game and configure it online to allow you to make HTTP requests and access the Data Store APIs. So be sure to look at the section in that link titled, Using Data Stores in Studio, it will walk you through the menus.
Anyways, right now, you are creating the player's starting values when they join the game. DataStores allow you save the values from the last session and then load those in the next time they join.
Roblox DataStores allow you to store key-value tables. Let's make a helper object for managing the loading and saving of data.
Make a ModuleScript called PlayerDataStore :
-- Make a database called PlayerExperience, we will store all of our data here
local DataStoreService = game:GetService("DataStoreService")
local playerStore = DataStoreService:GetDataStore("PlayerExperience")
local PlayerDataStore = {}
function PlayerDataStore.getDataForPlayer(player, defaultData)
-- attempt to get the data for a player
local playerData
local success, err = pcall(function()
playerData = playerStore:GetAsync(player.UserId)
end)
-- if it fails, there are two possibilities:
-- a) the player has never played before
-- b) the network request failed for some reason
-- either way, give them the default data
if not success or not playerData then
print("Failed to fetch data for ", player.Name, " with error ", err)
playerData = defaultData
else
print("Found data : ", playerData)
end
-- give the data back to the caller
return playerData
end
function PlayerDataStore.saveDataForPlayer(player, saveData)
-- since this call is asyncronous, it's possible that it could fail, so pcall it
local success, err = pcall(function()
-- use the player's UserId as the key to store data
playerStore:SetAsync(player.UserId, saveData)
end)
if not success then
print("Something went wrong, losing player data...")
print(err)
end
end
return PlayerDataStore
Now we can use this module to handle all of our loading and saving.
At the end of the day, your player join code will look very similar to your example, it will just try to first load the data. It is also important to listen for when the player leaves, so you can save their data for next time.
In a Script next to PlayerDataStore :
-- load in the PlayerDataStore module
local playerDataStore = require(script.Parent.PlayerDataStore)
local function onPlayerJoin(player)
-- get the player's information from the data store,
-- and use it to initialize the leaderstats
local defaultData = {
gold = 150,
speed = 0,
coins = 0,
rebirths = 0,
}
local loadedData = playerDataStore.getDataForPlayer(player, defaultData)
-- make the leaderboard
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "JumpBoost"
gold.Value = loadedData.gold
gold.Parent = leaderstats
local speed = Instance.new("IntValue")
speed.Name = "Speed"
speed.Value = loadedData.speed
speed.Parent = leaderstats
local coin = Instance.new("IntValue")
coin.Name = "CloudCoins"
coin.Value = loadedData.coins
coin.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Value = loadedData.rebirths
rebirths.Parent = leaderstats
end
local function onPlayerExit(player)
-- when a player leaves, save their data
local playerStats = player:FindFirstChild("leaderstats")
local saveData = {
gold = playerStats.JumpBoost.Value,
speed = playerStats.Speed.Value,
coins = playerStats.CloudCoins.Value,
rebirths = playerStats.Rebirths.Value,
}
playerDataStore.saveDataForPlayer(player, saveData)
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
Hope this helps!

Related

ServerScriptService.leaderstats:87: attempt to index nil with 'leaderstats

So basically I'm making a roblox clicker simulator game and sometimes when I test the save data for clicks it sometimes gives me this error ServerScriptService.leaderstats:87: attempt to index nil with 'leaderstats'
local success, errormessage = pcall(function()
rebirthsDataStore:SetAsync(playerUserId, rebirthsValue)
end)
-- Saving Gems Data
local gemsValue = player.leaderstats.Gems.Value
local success, errormessage = pcall(function()
gemsDataStore:SetAsync(playerUserId, gemsValue)
end)
end)
game:BindToClose(function(player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player"..Player.UserId
-- Saving Clicks
local clicksValue = player.leaderstats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
-- Saving Rebirths
local rebirthsValue = player.leaderstats.Rebirths.Value
local success, errormessage = pcall(function()
rebirthsDataStore:SetAsync(playerUserId, rebirthsValue)
end)
-- Saving Gems Data
local gemsValue = player.leaderstats.Gems.Value
local success, errormessage = pcall(function()
gemsDataStore:SetAsync(playerUserId, gemsValue)
end)
end
end)
Your error is telling you that when you try to access player.leaderstats, the player variable doesn't have a value. And this is because the game:BindToClose function does not provide a Player as an argument.
But you are iterating over all of the Players left in the server in a for-loop, just set the name of that iterator variable to be player and make sure the capitalization is correct in the rest of your code.
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
local playerUserId = "player" .. player.UserId

Roblox Studio- attempt to index nil with 'leaderstats' error in output

so i want to make this so when i click it gives me a "Coin" but it does not work & says attempt to index nil with 'leaderstats'
`game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder', player)
leaderstats.Name = 'leaderstats'
local coins = Instance.new('IntValue', leaderstats)
coins.Name = 'Coins'
coins.Value = 0
end)
game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function()
local currency = 'Coins'
local amount = 5
player.leaderstats[currency].Value = player.leaderstats[currency].Value + amount
end)``
You forgot the "player" parameter in OnServerEvent which is why it's nil
fix:
game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player) --added player parameter
local currency = 'Coins'
local amount = 5
player.leaderstats[currency].Value = player.leaderstats[currency].Value + amount
end)

Bacula multiple storage device

It seems to be relatively a simple question. I have two mount points on bacula-sd (storage) server- one for local drive and one for s3 bucket (off side backup) and what I need is start two concurrent job at the same time on both drives. So far I have something like that in bacula devices.conf
Device {
Name = "example.prod.com"
Device Type = File
Media Type = File
Archive Device = "/data/bacula/example.prod.com"
LabelMedia = yes
Random Access = yes
AutomaticMount = yes
RemovableMedia = no
AlwaysOpen = no
Maximum Network Buffer Size = 65536
}
Device {
Name = example.prod.com-s3
Device Type = File
Media Type = File
Archive Device = "/mnt/bacula-backup-storage/example.prod.com-s3"
LabelMedia = yes
Random Access = Yes
AutomaticMount = yes
RemovableMedia = no
AlwaysOpen = no
Maximum Network Buffer Size = 65536
}
but it's probably not enough because the job started only on the first device. Do I need disk autochanger or something?
You have to change Media Type = ... parameter to be different for both Devices, i.e.
Device {
Name = "example.prod.com"
Device Type = File
Media Type = File
...
}
Device {
Name = example.prod.com-s3
Device Type = File
Media Type = S3
...
}
It won't work otherwise.

How do I Make a double jump Script that makes the power of the second Jump the Value of the players leader stat value

I made this code thinking it would allow the player to jump Twice and the second Jump be the power of its leader stat but Instead it doesn't even allow the player to Jump a second time.
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character
local humanoid
local canDoubleJump = false
local hasDoubleJumped = false
local oldPower
local time_delay = 0.2
local jump_multiplier = player.leaderstats.JumpBoost.Value
function onJumpRequest()
if not character or not humanoid or not
character:IsDescendantOf(workspace) or humanoid:GetState() ==
Enum.HumanoidStateType.Dead then
return
end
if canDoubleJump and not hasDoubleJumped then
hasDoubleJumped = true
humanoid.JumpPower = oldPower * jump_multiplier
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
local function characterAdded(new)
character = new
humanoid = new:WaitForChild("Humanoid")
hasDoubleJumped = false
canDoubleJump = false
oldPower = humanoid.JumpPower
humanoid.StateChanged:connect(function(old, new)
if new == Enum.HumanoidStateType.Landed then
canDoubleJump = false
hasDoubleJumped = false
humanoid.JumpPower = oldPower
elseif new == Enum.HumanoidStateType.Freefall then
wait(time_delay)
canDoubleJump = true
end
end)
end
if player.Character then
characterAdded(player.Character)
end
player.CharacterAdded:connect(characterAdded)
UIS.JumpRequest:connect(onJumpRequest)
I expected the player to Jump Twice with the second Jump having the power of the leader stat(I only put that and this because it says it wants more detail)
LocalScripts do not execute in game.Workspace: they only run on clients, hence the term 'local'; Scripts, on the other hand, are server-side.
You could use a Script to place your double-jump script--a LocalScript--into incoming players' character models.
-- Server-side Script
local players = game:GetService("Players")
local jumpScript = script:WaitForChild("JumpScript") -- Assume the double-jump script is a LocalScript called 'JumpScript'.
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
-- Check to make sure the character model does not already contain the double-jump script, just in case.
local clone = char:FindFirstChild("JumpScript")
if (not clone) then
clone = jumpScript:Clone()
clone.Parent = char
end
end)
end)
As a note, it is good practice to put server-side scripts like this into ServerScriptStorage instead of workspace or Lighting. (You can read about the safety of ServerScriptStoragehere.)

How to read specific numbers out of a file

I want to write a Lua script which will save and load my vars back into my program. I searched a bit in the Internet for code examples and now i have this:
--SetUp vars
accept = 1
strenght = 5
hp = 2
--create file
local f = assert(io.open("quicksave", "w"))
f:write(accept, "\n")
f:write(strenght, "\n")
f:write(hp, "\n")
f:close()
--Set vars to 0(simulate restart of program)
accept = 0
strenght = 0
hp = 0
print("accept: "..accept.." Strenght: "..strenght.." HP: "..hp)
--load in the saved vars
local f = assert(io.open("quicksave", "r"))
accept = f:read("*line")
strenght = f:read("*line")
hp = f:read("*line")
f:close()
print("accept: "..accept.." Strenght: "..strenght.." HP: "..hp)
This works fine for me, but how can I read only specific values from the file? For example: what should I do if i want to read out only the second line of the file (the var for strength)?
You can simply read and discard the first line:
--load in the second saved var
local f = assert(io.open("quicksave", "r"))
f:read("*line")
strenght = f:read("*line")
Nevertheless, I suggest you save your data as a Lua script that can be loaded with dofile. Something like:
return {
accept = 1,
strenght = 5,
hp = 2
}
Then you can load it into a local variable and read the fields you need:
local state = dofile("state.lua")
strenght = state.strenght