Overview
I am trying to make a Battle Royale-themed swording fighting game. There have been some issues saving the players swords (or loading in the swords. I am unsure what the problem is.)
Important facts:
-
DataStores are enabled with testing in Roblox Studio
-
No output Errors
-
The game has been thoroughly tested and not one time has the players swords shown on the GUI when rejoining the game
Very Detailed Pictures describing the problem
When I am in the game and I purchase the sword.
I leave the game, go back in and all of my data is missing.
When I purchase a sword the sword goes into my inventory. When I re-join the game, the sword is no longer in the inventory. (Rainbow sword is an example.)
Code that is related to the supposed issue
I’m sorry for the incorrect formatting. Formatting on the Dev forum is very difficult. Anyways, here is a script in ServerScriptService which saves the players data.
game.Players.PlayerAdded:Connect(function(player)
playersLeft = playersLeft + 1
-- Other code here not useful to the swords PM if you need to know what I scripted here
-- Data Stores
local player_data
local weaponsData
local equippedData
pcall(function()
player_data = dataStores:GetAsync(player.UserId.."-Bucks") --12787687-Bucks
end)
pcall(function()
weaponsData = dataStores:GetAsync(player.UserId.."-Weps") --12787687-Bucks
end)
pcall(function()
equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue") --12787687-Bucks
end)
if player_data ~= nil then
-- Player Has saved , load it in
bucks.Value = player_data
else
-- New Player
bucks.Value = defaultCash
end
if weaponsData then
for _, weapon in pairs(weaponsData) do
if game.ServerStorage.Items:FindFirstChild(weapon) then
local weaponClone = game.ServerStorage.Items[weapon]:Clone()
weaponClone.Parent = inventory
print(weapon.." loaded in!")
end
end
if equippedData then
equipped.Value = equippedData
player:WaitForChild("PlayerGui")
game.ReplicatedStorage.SendEquipped:FireClient(player,equippedData)
end
else
print("No weapons Data")
end
end)
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
dataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)
end)
pcall(function()
local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildern()
local weaponsTable = {}
for _, v in pairs(weapons) do
table.insert(weaponsTable,v.Name)
end
dataStores:SetAsync(player.UserId.."-Weps",weaponsTable)
if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
local equippedVal = game.ServerStorage.PlayerData[player.Name].Equipped
dataStores:SetAsync(player.UserId.."-EquippedValue",equippedVal.Value)
end
end)
print("Saved Weapons and Bucks")
playersLeft = playersLeft - 1
bindableEvent:Fire()
end)
game:BindToClose(function()
-- This will be triggered upon shutdown
while playersLeft > 0 do
bindableEvent.Event:Wait()
end
end)
If the datastores are working properly then below I will put when the script when the swords are put in the shop UI.
I learned most of the datastores and how to save the swords from and @Alvin_Blox tutorial:
I understand If I may be asking for a lot, but this problem has been an issue for over a month so I thought this was the right time to ask the community for help. If it seems cheezy that I am using a tutorial please keep in mind that I am still learning Lua and this tutorial has helped a lot.
If you need more scripts or need to see the game yourself please DM me.