Greetings! I’m creating an RPG game with a sword-saving system. Whenever the player leaves, I add all of the swords into a table, however, this table is nil once the player rejoins.
I couldn’t really add the full script hence the length
FYI, once the sword enters a player’s inventory, I created a string value holding the names of the swords (I later use the names to grab it out of server storage) this explains the ‘player.SwordSaver:GetChildren()’
local dataStore = game:GetService("DataStoreService")
local savedData = dataStore:GetDataStore("savedData")
local timesRan = 0
local timesRan1 = 0
local serverStorage = game:GetService("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
if not player:FindFirstChild("SwordSaver") then
-- Include folder for swords
local swordSaver = Instance.new("Folder", player)
swordSaver.Name = "SwordSaver"
end
local char = player.Character or player.CharacterAdded:Wait()
local backpackTools = player.Backpack:GetChildren()
local charTools = char:GetChildren()
local data
-- Loop through, and create the destined amount of string values
for _, tool in pairs(backpackTools and charTools) do
if tool.ClassName == "Tool" then
if not player.SwordSaver:FindFirstChild(tool.Name) then
local swordString = Instance.new("StringValue", player.SwordSaver)
swordString.Name = tool.Name
end
end
end
local savedInstances = {}
for _, tool in pairs(player.SwordSaver:GetChildren()) do
print(tool)
table.insert(savedInstances, tool.Name)
end
local success, err = pcall(function()
data = savedData:GetAsync(player.UserId, savedInstances)
print("test")
for i, v in pairs(data) do
print(v)
end
end)
if success then
for _, tool in pairs(data) do
print(tool)
local newSwords = serverStorage.Swords[data]:Clone()
newSwords.Parent = player.Backpack
local newString = Instance.new("StringValue", player)
newString.Name = tool
end
end
end)
-- Finalizes the sword once the player leaves
game.Players.PlayerRemoving:connect(function(player)
local savedInstances = {}
for _, tool in pairs(player.SwordSaver:GetChildren()) do
print(tool)
table.insert(savedInstances, tool.Name)
end
savedData:SetAsync(player.UserId, savedInstances)
end)''''
local dataStore = game:GetService("DataStoreService")
local savedData = dataStore:GetDataStore("savedData")
local serverStorage = game:GetService("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local data
local success, err = pcall(function()
data = savedData:GetAsync(player.UserId)
for i, v in pairs(data) do -- this is for you to debug
print(v)
end
end)
if success then
for _, tool in pairs(data) do
print(tool)
local newSwords = serverStorage.Swords[data]:Clone()
newSwords.Parent = player.Backpack
end
end
end)
game.Players.PlayerRemoving:connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local backpackTools = player.Backpack:GetChildren()
local charTools = char:GetChildren()
local savedInstances = {}
for _, tool in pairs(backpackTools) do
if tool.ClassName == "Tool" then
savedInstances[#savedInstances+1]=tool.Name
end
end
for _, tool in pairs(charTools) do
if tool.ClassName == "Tool" then
savedInstances[#savedInstances+1]=tool.Name
end
end
savedData:SetAsync(player.UserId, savedInstances)
end)
Let me know if you would like me to explain what I changed.
That’s fair, so cheers, but yeah it’s those tiny details that you really gotta look out for when something’s not working right. Probably is the original issue.