DataStore Value Problem; Shows Wong Value

Dear Developers,
I have an issue with my DataStore, I have adjusted it so the value has to be Default Gun or Default Knife, but it is still showing 0 as the value…
What am I supposed to do?

local datastore = game:GetService(“DataStoreService”)
local ds1 = datastore:GetDataStore(“KnifeSaveSystem”)
local ds2 = datastore:GetDataStore(“GunSaveSystem”)

game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new(“Folder”, player)
folder.Name = “Equip”

local Knife = Instance.new("StringValue", folder)
Knife.Name = "Knife"
Knife.Value = "Default Knife" -- Does not work, Value is still 0
																																																																																																			
local Gun = Instance.new("StringValue", folder)
Gun.Name = "Gun"
Gun.Value = "Default Gun" -- Does not work, value is still 0
																																									
Knife.Value = ds1:GetAsync(player.UserId) or 0
ds1:SetAsync(player.UserId, Knife.Value)

Knife.Changed:Connect(function()
	ds1:SetAsync(player.UserId, Knife.Value)

end)

Gun.Value = ds2:GetAsync(player.UserId) or 0
ds2:SetAsync(player.UserId, Gun.Value)

Gun.Changed:Connect(function()
	ds2:SetAsync(player.UserId, Gun.Value)
end)

end)

Couple of fundamental issues with your code before addressing why your code isn’t working.

  • Using two DataStores for something that can be consolidated into one DataStore. You will hit DataStore limits quickly. Use one DataStore where the key is the UserId and the value is a two-key dictionary for the knife and the gun.

  • Stop using Instance.new’s parent argument.

  • Do NOT use ANY DataStore method with the Changed event. You are not only going to hit the write request limit, but your write requests themselves will get throttled because you cannot write to the same key within 6 seconds of each other. Save data via PlayerRemoving.

  • Lack of error handling. While this will give users who have no data a value of 0 for their gun and knife, it will also give 0 if DataStores are experiencing an outage. Players will lose their data and you will overwrite it such that progress cannot be reverted.

These are all important issues you need to fix with your DataStore script which in turn will resolve the issue of you receiving improper values. It is also worth noting that GetAsync does cache the value for 4 seconds (or so I remember); despite this, you will still consume a get request when using GetAsync.

I’ve given you the steps, so it’s up to you to do the research and attempt to incorporate this feedback into your system if you choose to do so. Feel free to post questions if you need help on implementation after you’ve made an attempt to do so.

If you are unfamiliar with DataStore workings, then a common suggestion and time investment would be to look into DataStore2. This module will do the heavy lifting for you, so all that’ll be required of you is to use its API properly. This is a good module for beginner and advanced developers not only to simplify the process (and let Kampf do all our hard work), but also due to the features it provides.

2 Likes