Essentially I my game has an item that you can click to obtain it. To elaborate the title, I want to make it so that if you have the item and haven’t died yet, the item stays with you even when you teleport to another place and rejoin. I know this probably used datastores but im not sure how it would translate that to another game.
thanks
To do this you’d need to serialize your tools into something that the datastore can save and read, for this script I’m going to be using StringValues.
I’ve provided a very bare-bones (but probably functional (can’t currently test it)) script below.
This script assumes that your tools are parented under a folder named “Tools” in ServerStorage, and to save tools you’ll create a new StringValue with the same name as your tool in ServerStorage under the folder named “Tools” that will be in every player.
local datastoreservice = game:GetService("DataStoreService")
local ds = datastoreservice:GetDataStore("GameData")
game.Players.PlayerAdded:Connect(function(plr)
local tools = Instance.new("Folder")
tools.Name = "Tools"
tools.Parent = plr
local data = ds:GetAsync(plr.UserId)
if data then
if data.tools then
for i, v in pairs(data.tools) do
local new = Instance.new("StringValue")
new.Name = v
new.Parent = tools
end
end
end
plr.CharacterAdded:Connect(function()
for i, v in pairs(tools:GetChildren()) do
if game.ServerStorage.Tools:FindFirstChild(v.Name) then
game.ServerStorage.Tools:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
end
end
end)
end)
function save(plr)
local data = {}
data.tools = {}
for i, v in pairs(plr.Tools:GetChildren()) do
table.insert(data.tools, v.Name)
end
ds:SetAsync(plr.UserId, data)
end
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
for i, v in pairs(game.Players:GetChildren()) do
save(v)
end
end)
while true do
task.wait(300)
for i, v in pairs(game.Players:GetChildren()) do
save(v)
end
end