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)
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.
Use WaitForChild because the PlayerGui will not load instantly - it will load after that code has already run.
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
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.
Save player data when they leave the game and at regular intervals.
Add a BindToClose() function to ensure that the data store request is fully complete.
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 . I hope this helps!
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.