So,
I have been trying to make a script that saves the player’s weapon inventory (NOT THE PLAYER BACKPACK) , and loads it. But, the problem is that my script is not working and i can not find out why, can anyone help me?
note i basically created a folder named Values into the player, and a folder named inventory into this folder, saved guns are put into the inventory folder
here’s my script
local DataStoreService = game:GetService("DataStoreService")
local InventoryStore = DataStoreService:GetDataStore("Inventory")
game:GetService("Players").PlayerAdded:connect(function(plr)
pcall(function()
local Guns = InventoryStore:GetAsync("User-"..plr.UserId)
print("finding guns")
if Guns then
print("Found save")
for i,v in pairs(Guns) do
print("Adding saved guns to inventory")
local gunfound = game.ReplicatedStorage.Weapons:FindFirstChild(v)
if gunfound then
print("Placed a gun in the players inventory")
gunfound:Clone().Parent = plr.Values.Inventory
print(gunfound.Name)
end
end
end
end)
end)
game:GetService("Players").PlayerRemoving:connect(function(plr)
pcall(function()
print("Began saving")
local gunsave = {}
for i,gun in pairs(plr.Values.Inventory:GetChildren()) do
if gun then
print("Added gun to save")
table.insert(gunsave,gun.Name)
end
end
print(unpack(gunsave))
InventoryStore:SetAsync("User-"..plr.UserId,gunsave)
print(InventoryStore)
end)
end)
You have print statements throughout so which statement does it get to before it stops?
What is the outcome and how does it differ to what you expected?
With things wrapped in pcall you may find it errors and you never knew. You should always read the return values of a pcall and if it has failed, print the message for debugging purposes.
Okay so it seems like it doesn’t get to the point where it actually saves, or it fails at that exact part. You can see that as it doesn’t print the InventoryStore.
So print the error message from the failed pcall as I suggested in the second half of my last reply.
local success, message = pcall( yourfunc )
if not success then
warn( message )
end
Ok So you really should have this serverside. A hacker can just change the value and save what ever they want.
The server can clone the gun and parent it to the player.
The player’s local script can handle the work with the gun.
You have a folder on the server storing all the info about what the players have. That info is saved when the player leaves.
If the player leaves the game, then the player doesn’t exist and can’t run code for itself. You want the server to go “Oh that player isn’t connected anymore. I better save their data.”
I think that would save about 50% of your issues or more.
You have some errors in there like 2 ends and 2 end)s but I think that’s just formatting on the forum.
Once you sort those out you can look at sorting the next stuff if there are any problems.
In your PlayerAdded Event, don’t call the whole code as a protected call(PCall). Instead, only use PCall for when you’re retrieving the data from DataStore:GetAsync() and return the data in a PCall. This is shown below.
local success, guns = pcall(function()
return InventoryStore:GetAsync("User-"..plr.UserId)
end
if success then
print(unpack(guns))
end
One more thing would be great. If you could print something like “Saves!” after this line below which is in the PlayerRemoving Event, it would help out a bit. See if it prints or not.