so basically I made a system where you can buy knives in a shop and i suck with datastores and what im trying to do is save all of the knives without typing all of the knives out individually (the knives are supposed to be stored as a numbervalue that has the name “hasKnifenamehere”) can anybody help me?
local knivesFolder = path.to.thing
local dataToSaveToDatastore = {
knives = {}
}
for _,value in knivesFolder:GetChildren() do
dataToSaveToDatastore.knives[ value.Name ] = value.Value
end
This will result in a dictionary where the key is the name of a knife and the value the value. ({[string]: number}
)
The easiest way is to store all of the knifes in an array.
Commonly i see a lot of beginners storing stuff inside a folder thats parented to the player and looping through the folder and saving each value when the player leaves.
This works. But isnt the best way. Because if the code runs after the player has left the game it’ll break.
Heres a quick example:
Make a module named “Knifes” for example. Return an empty table in the module.
return {}
When the player joins. Add an dictionary to it with the players userid and remove it when the player leaves. We’ll be refering to this dictionary as the “Players Storage” for this topic.
local Storage = require(game.ServerScriptService.Knifes)
game.Players.PlayerAdded:Connect(function(Player: Player)
Storage[Player.UserId] = {}
end)
game.Players.PlayerRemoving:Connect(function(Player: Player)
Storage[Player.UserId] = nil
end)
Whenever the player purchases something lets say through a remote event. Add the knife name to the players storage.
local SomeEvent = game.ReplicatedStorage.Event
local Storage = require(game.ServerScriptService.Knifes)
SomeEvent.OnServerEvent:Connect(function(Player: Player, NameOfKnife: string)
local PlayerStorage = Storage[Player.UserId]
if PlayerStorage then -- // Just an extra security check. Just makes sure the dictionary exists.
table.insert(PlayerStorage,NameOfKnife)
end
end)
Whenever the player leaves the game. Save the players storage to a datastore.
local SomeEvent = game.ReplicatedStorage.Event
local Storage = require(game.ServerScriptService.Knifes)
game.Players.PlayerRemoving:Connect(function(Player: Player)
local PlayerStorage = Storage[Player.UserId]
if PlayerStorage then
-- // Save array to data store.
Storage[Player.UserId] = nil
end
end)
how would i save the array to a datastore tho? i searched it up and it seemed to not work. as if it was impossible
try to change your knife value into table with name, equipped boolean, damage ect, and when player joins check data and look for knife value if value then …
i was talking about the solution @Herobrinekd1 told me, i printed the value when i bought a couple of knives and it gave me:
{
[1] = "Pencil Knife",
[2] = "Fire Knife",
[3] = "Melon Knife"
}
now that’s what i am trying to save to a datastore and as i said i suck with datastores its my “biggest enemy”
Its pretty simple axtually. All you’ve got to do is type the following:
Though also be aware that each “SetAsync()” call on a key has a 6 seconds cooldown. If you attempt to perform another set request under that time period the request will be put onto a queue.
local Service = game:GetService("DataStoreService")
local MyStorage = Service:GetDataStore("Test Storage") -- // Your DataStore name.
local MyDictionary = {
["Herobrinekid1"] = {
"Pencil Knife",
"Fire Knife",
"Melon Knife",
}
}
local UserId = 36533345 -- // Using myself for demonstration X)
local Sucess: boolean, Res: boolean = pcall(MyStorage.SetAsync,MyStorage,UserId,MyDictionary)
--[[
Same as doing:
local Sucess, Res = pcall(function()
MyStorage:SetAsync(UserId,MyArray)
end)
--]]
-- // I just prefer using a pcall that way X)
-------------------------------------------
-- // For debugging purposes.
if Sucess then
warn("Yay!! my array was saved :D")
else
warn("My array wasnt saved D:")
warn(Res)
end
gives me: Argument 2 missing or nil
local Service = game:GetService("DataStoreService")
local Datastore = Service:GetDataStore("Knives") -- // Your DataStore name.
game.Players.PlayerAdded:Connect(function(plr)
local Storage = require(game.ServerScriptService.ModuleKnives)
local saveditems = Datastore:GetAsync(plr.userId)
if saveditems then
local PlayerStorage = Storage[plr.UserId]
PlayerStorage = saveditems
else
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Storage = require(game.ServerScriptService.ModuleKnives)
local PlayerStorage = Storage[plr.UserId]
local Sucess, Res = pcall(function()
Datastore:SetAsync(plr.UserId,PlayerStorage)
end)
if Sucess then
warn("Yay!! my array was saved :D")
else
warn("My array wasnt saved D:")
warn(Res)
end
end)
Use folders and tables also some stringvalues to save player knives in the said folder.
what do you mean? can you be a bit more specific?
Loop through the stringvalues (of which contain the skin data like name) then save them.
0 = Not owned
1 = Owned
just how do i fix this bug? i already have someone who helped me but i need help fixing that bug
What piece of code gives you the error?
There’s a module I know that is able to convert an instance to a table or convert that table to an instance while keeping all the same properties the instance had before. Here it is: Converter Module: Instance ↔ Table | Save Instances to DataStores and More!
I use it for my building game where players have plots and they could build stuff from blocks on them with different block shapes, colours and materials. This module has saved me so much time and brainstorms, I am very thankful for it even being a thing.
The best thing is that it works lightning fast and doesn’t even lag.
Here are the features:
- Supports the DataStore/DataStore2 format.
- Supports object variables†.
- Supports Workspace and Lighting properties.
- Can be used without HTTP Service.
- Many customizable settings available.
- Complete backwards compatibility.
- Full attribute support.
- Supports ALL data types (CFrame, String, Vector3, UDim2, Ray, Faces, etc.)
- Compatible with the ModuleScripts created from the Instance to ModuleScript Converter plugin.
it’s not an instance, it’s a module that includes the names of the wep
no line, its doing the
warn(Res)
part
Well, earlier you talked about knives, what’s up?
actually, do you know how i would make a system that stores how many knives you have? i suck at datastores so i need really good explanation. also you are supposed to be able to have more than 1 knife.
You can just use IntValues/NumberValues.
though won’t that make me have to create unnecessarily many intvalues?