I’ve recently been trying to optimize this script to save the players inventory upon them dying, and leaving/joining the game.
The one issue with it is that with the way Roblox is, if the player equips the tool and it gets parented to the Character in workspace as usual, the script does not recognize it, and therefor skims past it while saving the tools. This causes a major issue now, as if the player spams 1-9 on their keyboard and equips all the tools and unequips them while dead, upon respawn they will lose nearly all of them.
I’ve tried going about this with workarounds such as force unequipping all the tools on death with Humanoid:UnequipTools() and making the tools forcibly unequip themselves if the player is found as dead with a value and script within the tool, but both can be bypassed if the player keeps spamming the tool, so they clearly aren’t very good workarounds. If there are any types of events I can use to save the inventory last second while they’re dead like a character.Respawning event that I am not aware of, please do tell how you would go about fixing this. It’s probably super simple and something I skimmed over.
I may have miscalculated. After debugging and trying to test the limits of this, it seems as though if you leave whilst in the middle of the items transferring from the folder to the players backpack, it deletes all the items. As well as this, if you equip an item and reset with it equipped, it duplicates it. I tried redoing the inventory save system as to save the folder I made in the player instead of the Backpack, and putting all the items in the folder when you die, leave, etc. but this ended to no avail. You run into this at all?
I completely forgot about that lol. Fortunately I already fixed this as well. This will need some script organization though.
So first you need to try to make the DataStore a module and call saving/loading data manually (call in the PlayerAdded/Removing events, handled in another script). With this you can save data after placing the tools into the folder.
For the moving function, have the player unequip tools then move the tools to the folder. Call this function on player died, characterRemoving and playerRemoving. It would be something like this:
local function MoveTools()
--char:UnequipTools()
--move tools in backpack to folder
end
-- call function when player dies, player's character gets removed and player removing
game.Players.PlayerRemoving:Connect(function(plr)
MoveTools()
-- save data function (where turning datastore into module becomes helpful)
end)
This may break your script structure, but I’m not sure. It’s pretty long so feel free to ask me if you have any problems!
I’ve been modifying the datastore quite a lot, and I’ve figured out that currently, the script works fine in every way. Though, I found this out when I reset and left after fixing it to save using the folder I made, and not the backpack. Through this, I reverse engineered my way of thinking completely and simply made it so when you leave, both the folders save in the data, and upon rejoining, it empties out the custom made folder and places it within the players inventory. All I need to do now is make it to where the Humanoid de-equips the characters held out items when leaving the game, and that’s turning out to be quite a struggle as well. I thought that a simple
would be enough to de equip all their tools once leaving the game with one equipped, but it is still deleting the one they are holding out. It unequips their tools, as I just put in a check to see if it worked or not, but I think it’s more on the data’s issue that it doesn’t detect the one they are holding out for some reason, even though I’m unequipping it. Any luck you have this fix as well?
Seems like my brain farted a little bit. I thought from prior events that PlayerRemoving fired after the character was already deleted, but that wasn’t the case this time around. Thanks for helping out!
I cut to the chase too early yet again, like I said previously, the character is actually deleted when PlayerRemoving is fired. This means that my datastore tricked me into thinking it was saving the tools with the fix you provided, but in reality, it was simply giving me the old save key since it error’d every time it tried to Save() on the playerremoving event. Basically, it didn’t work. You think theres any possibility to fix this with CharacterRemoving still?
That’s weird, it’s probably coming from your DataStore. My datastore using ProfileService worked though. You probably need to fix that or use a new one (if your game isn’t published), because erroring on PlayerRemoving is dangerous.
It only error’d when I tried to grab the player.Character in that event. It’s because the character is removed by the time PlayerRemoving is fired, but I might just rework the datastore like you said.
Sadly its not that easy, as I’m not making a game that uses something as simple Starter Gear. It’s more of a hardcore game and you aren’t meant to spawn in with anything and keep it throughout your entire gameplay.
I actually already solved it with a little encouragement from neweve2323, but it was basically a Rogue Lineage styled game with items you keep through leaving and resetting, but being killed by someone else would mean you lose all you gained. Thanks for trying to help anyways
You can still use startergear by wiping all the gears from the player starter gear if the player was killed by another player but DataStore works the same.