My goal is to be able to add different items like for example: Tools, Vehicles, and other models that can be furniture and house models, to my DataStore.
My script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Game") -- Change the name to whatever you want, if you change it with existing data on th ename, it will NOT carry over.
game.Players.PlayerAdded:Connect(function(player)
local Data = DataStore:GetAsync("Player_"..player.UserId)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new('IntValue', leaderstats)
Money.Name = "Money"
Money.Value = 0
local Level = Instance.new('IntValue', leaderstats)
Level.Name = "Level"
Level.Value = 0
local Exp = Instance.new('IntValue', Level)
Exp.Name = "Exp"
Exp.Value = 0
local max = Instance.new('IntValue', Level)
max.Name = "max"
max.Value = 0
if Data then
Money.Value = Data["Money"]
Level.Value = Data["Level"]
Exp.Value = Data["Exp"]
max.Value = Data["max"]
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local Data = {
Money = player.leaderstats.Money.Value;
Level = player.leaderstats.Level.Value;
Exp = player.leaderstats.Level.Exp.Value;
max = player.leaderstats.Level.max.Value;
--Add any other variables you want!
}
local success, err = pcall(function()
DataStore:SetAsync("Player_"..player.UserId, Data)
end)
if err then
warn("There was an error saving data:", err)
end
end)