I can't get the data of datastore

Hello everyone, I was trying to make a save slots for my game and I made a thing to change the name for slots, It saves but the problem is I can’t get the data and I don’t know what to do and it doesn’t print anything. Any help is appreciated

Code:

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local autoSavingStore = dataStoreService:GetDataStore("NameSave")
local slotV = nil


game.ReplicatedStorage.SaveName.OnServerEvent:Connect(function(plr, slot, text)
	local slotV = slot
	local key = tostring(plr.UserId).." -slot"..tostring(slot)
	local success1 = pcall(function()
		autoSavingStore:SetAsync(key, text)
	end)
	if success1 then
		print("Saved")
	end
	print(slotV)
end)

game.Players.PlayerAdded:Connect(function(p)
	local key = tostring(p.UserId).." -slot"..tostring(slotV)
	if p.PlayerGui:FindFirstChild("SavingUI") then
		local data
		local success = pcall(function()
			data = autoSavingStore:GetAsync(key)
		end)
		
		if success then
			print("works")
			p.PlayerGui:FindFirstChild("SavingUI").MainFrame.SaveList:FindFirstChild("Slot"..tostring(slotV)).SlotTitle.Text = data
		else
			print("failed")
		end
	end
end)
1 Like

If nothing prints when PlayerAdded is fired, it is likely that

p.PlayerGui:FindFirstChild("SavingUI")

is equal to false. Is there a reason this statement is here?

i added an else statement and it printed something while it exists, how can i fix that?

Are there any errors in the output? If not what’s the event SaveName connected to? Like the local scritp?

(I’m not super experienced but I hope I maybe can help you out)

There is no errors in the output, SaveName is connected by a localscript in the UI when textbox lost focus.

Maybe you should use WaitForChild since it is a localscript

plr.PlayerGui:WaitForChild("SavingUI")
print("Loaded")

Refrence:

WaitForChild() is extremely important when working on code run by the client in a LocalScript. The Roblox engine does not guarantee the time or order in which objects are replicated from the server to the client. Additionally, if an experience has Workspace.StreamingEnabled set to true, BaseParts that are far away from the player’s character may not be streamed to the client, potentially causing scripts to break when indexing objects that do not yet exist on the client.

I am going to take part of @slendarcoolfan 's answer, and give you a few tips for your script.

  1. Use WaitForChild because the PlayerGui will not load instantly - it will load after that code has already run.
  2. Do not use a RemoteEvent to control when data is saved. Clients can spam the event as well as edit the data to whatever they want. This can result in:
  • Exploited data
  • Data budget being used up really fast
  • Game crashing due to DoS/DDoS attack
  1. Do not use the player gui to store values that need to be saved. Use some kind of instance value in the player, or an attribute assigned to the player and only ever edit this value and read this value on the server when modifying data.
  2. Save player data when they leave the game and at regular intervals.
  3. Add a BindToClose() function to ensure that the data store request is fully complete.
  4. Make more robust data saving code to ensure player data is not lost. This includes using UpdateAsync, data versioning, and multiple attempts at saving.

There are a lot of things here, please let me know if you want me to explain any of them :slight_smile: . I hope this helps!

nevermind i got it, i just used my save system to do this, mb for everyone thanks for the help tho.

Glad you managed to fix it. Please also consider implementing some of the suggestions I listed above, they will help to defend your game against exploiters. The structure you currently have is easily exploitable.

1 Like

yeah sure thing

OnlyTwentyCharacters

1 Like

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