Hello! I’m new to scripting and i need some help with my datastore script. I would like to save value without it showing in leaderstats. It’s for simulator backpack so it can change the value whenever someone buys new backpack and gets more room for their “water”. I’ve watched many Youtube tutorials on how to make simulator backpack but there is no tutorial on how to save the backpack.
I just want to save value without it showing in the leaderstats and please say if you know better way to make backpack for simulator game.
Here is my script:
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local water = Instance.new("IntValue")
water.Name = "Water"
water.Parent = leaderstats
local backpack = Instance.new("IntValue")
backpack.Name = "Backpack"
backpack.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
money.Value = data['Money']
water.Value = data['Water']
backpack.Value = data['Backpack']
else
money.Value = 0
water.Value = 0
backpack.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)
And i want to remove this from here: