So if i bought a weapon in my game it saves, but somtimes when i buy a weapon and change servers, the server that i bought the weapon on will have it, but the server i just joined wont?
is there a way i can prevent this with some sort of safety script?.
here is the short version with all the parts that link to the weapons.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local playerData = Instance.new("Folder")
playerData.Name = player.Name
playerData.Parent = game.ServerStorage.PlayerData
local equipped = Instance.new("StringValue")
equipped.Name = "Equipped"
equipped.Parent = playerData
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
inventory.Parent = playerData
local weaponsData
local equippedData
local success, errormessage = pcall(function()
weaponsData = myDataStore:GetAsync(player.UserId.."-Weps")
equippedData = myDataStore:GetAsync(player.UserId.."-EquippedValue")
end)
pcall(function()
weaponsData = myDataStore:GetAsync(player.UserId.."-Weps")
end)
pcall(function()
equippedData = myDataStore:GetAsync(player.UserId.."-EquippedValue")
end)
if weaponsData then
for _, weapon in pairs(weaponsData) do -- {"Sword3","Sword2","Sword","Icegun"}
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 -- No weps, no equips!
equipped.Value = equippedData
player:WaitForChild("PlayerGui")
game.ReplicatedStorage.SendEquipped:FireClient(player,equippedData) -- Send the player's equipped item to the client to display as GUI
end
else print("No weapons data") end
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
local weaponsTable = {}
for _, v in pairs(weapons) do
table.insert(weaponsTable,v.Name)
end
myDataStore:SetAsync(player.UserId.."-Weps",weaponsTable)
if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
local equippedSaveValue = game.ServerStorage.PlayerData[player.Name].Equipped.Value
myDataStore:SetAsync(player.UserId.."-EquippedValue",equippedSaveValue)
end
end)
print("Saved weapons and points")
bindableEvent:Fire()
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)