Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Unable to assign property Text. string expected, got nil (?)

Hello! I am working on a simple DataStorage that saves the text the player put Inside of a textbox and loads It. The problem Is that when you are trying to load the data (Aka the textbox text) an error appears saying:

Unable to assign property Text. string expected, got nil

Slot script:

local slot = script.Parent
local LoadButton = slot.LoadButton
local SaveButton = slot.SaveButton
local LoadFunction = game.ReplicatedStorage:WaitForChild("LoadSlot")
local SaveEvent = game.ReplicatedStorage:WaitForChild("SaveSlot")
local Textbox = game.Players.LocalPlayer.PlayerGui:WaitForChild("TrollStorage").TextBox

SaveButton.MouseButton1Click:Connect(function()
	SaveEvent:FireServer(slot.Name, Textbox.Text)
end)

LoadButton.MouseButton1Click:Connect(function()
	local data = LoadFunction:InvokeServer(slot.Name)
	Textbox.Text = data
end)

ServerScriptService:

local SaveSlot = game:GetService("ReplicatedStorage").SaveSlot
local LoadSlot = game:GetService("ReplicatedStorage").LoadSlot
local Datastore = game:GetService("DataStoreService")
local slotStore = Datastore:GetDataStore("SlotStore")


function LoadSlot.OnServerInvoke(player, slot)
	return slotStore:GetAsync(player.Name.."-"..slot)
end

SaveSlot.OnServerEvent:Connect(function(player, slot, data)
	local Success = false
	while not Success do
		Success = pcall(function()
			slotStore:GetAsync(player.Name.."-"..slot, data)
		end)
	end
end)

2 comments

0

GetAsync() will attempt to retrieve data. When you are saving something, you want to use SetAsync() or UpdateAsync() depending on the purpose.

0

Alright, but where should I place SetAsync() or UpdateAsync()?

1 answer

On your ServerScriptService script, instead of doing

slotStore:GetAsync(player.Name.."-"..slot, data)

on line 15, consider doing this:

slotStore:SetAsync(player.Name.."-"..slot, data)

Just like @PhantomVisual said in the comments, GetAsync is used to "get" or "load" data, and SetAsync will be "set" or "save" data.

This discussion is preserved as it was. New answers, comments, and votes are closed.