Saving values for weapons

So I have a shop system where people can buy swords. I want the purchase to save and be loaded when they rejoin so instead of prompting them to puchase again it can just say equip, and for this I had a bool value in each sword (like below)
image
I have a module script I changed from my normal playerData script to solely save the sword info so I can put it in a seperate DataStore (though that might be useless)


The autosave every 60 seconds works, but if the player leaves the game and the save function runs for that then it returns with this error.

I imagine it’s because the player’s character left so it can’t find the playergui but that complicates things since I don’t want to have to autosave every 10 seconds.

You can try making it save using


game.Players.PlayerRemoving:Connect(function()
  --add save code here
end)

1 Like

I recommend you create a list/table or a folder serverside that is created when the player has joined then add all the boolvalues/etc. you want to it and change it to according to if they purchased or not, and when the player leaves before you :Destroy() the folder, save the data in the folder/table/list.
For reference: Players.PlayerRemoving | Documentation - Roblox Creator Hub

I’m not 100% sure about this, but I think playerGui is destroyed before PlayerRemoving is called, therefore it cannot find the data anymore.

I was gonna mention SPWarriorGamer’s Idea, but he already posted it so go use that

1 Like

I think it would be better to have it in ReplicatedStorage so that the player can also access it without having to call the server to check

1 Like

Yes, that would work, I didn’t specify where to parent the folder/data, I just mentioned it to be created/saved serversided.

So I’ve made the folder in ReplicatedStorage and when a player joins it creates another folder in that folder under that player’s name.
image
And I’ve also changed the module so that it just saves any value inside the player’s folder as true or false.


Now I just need to create each bool value for each sword I have, but what method should I use for that. I was thinking of using for in pairs in the startergui to see how many swords there are and just make that many bool values but I was wondering if there’s a better way.


I did it like this in my leaderstats when the folder is made and it seems to work
image
Thanks for the help guys

1 Like

Thats one way to create the boolvalues, I’m not entirely sure there is a better way unless you manual change the number of sword bools the system produces each time you create a new sword (but this would be time consuming). In the end I would use a “for loop in pairs” for this, however you can also consider that boolvalues aren’t the only way to save data, you could have a script store a list that data is stored in, and if the client needs to read the data, you could probably send it with remoteEvents, however the client won’t be able to directly access the data as it needs to go through the server and wait for it to give it readable data.

Now, using a folder with bool values is not a bad idea to save your data, but there is no point for the client to have access to the readable bool values. In order to understand this, we must venture towards client manipulation. Where, exploiters and hackers can change the bool values to say something else, and if you have a local script it will show the change, HOWEVER the server will not accept these client changes (if FilteredEnabled is on, basically prevents hackers from accessing server data and taking ownership), so if you want the client to read the data to check if the player has PURCHASED the sword, it is not optimal, I would just run the script through the server and make it check if the player has access to the sword, I WOULD NOT advice you take this action through a local script.

An in pairs loop should work fine, I’m unsure if there’s any other way besides saving it all inside a script instead.

A good idea would be to save only owned swords and then apply all the values to all the sword bools, so if you add new swords it wont error when you expect a certain table length

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.