How do I make a text box update a object in game and save itself in a data store?

I’m trying to make a script that saves the text you put into a text box and also changes a text label in game

The issue I’m having is being unable to get the player from the mousebutton click which then causes a whole list of problems

I have tried looking though other posts but none of them I have found relate to what I’m trying to do.

I’m new to scripting so I don’t have much knowledge on data stores (this is my first time using them)
image

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local StallDataStore = DataStoreService:GetDataStore("StallDataStore")

script.Parent.MouseButton1Click:Connect(function(player)
	local StallSettings = player.PlayerGui.Start.Settings.Stall
	local StallName = StallSettings.NameBox.TextBox
	local Timer = 0

	--Name Data Save
	if Timer == 0 then
		local success, errormessage = pcall(function()
			StallDataStore:SetAsync(player.UserId.."-StallName",StallName.Text)

		end)

		if success then
			print("SavedName")
		else
			print("failed")
			print(errormessage)
		end

	else
		
		wait(1) 
		Timer = - 1 
		print(Timer)
	end

end)

MouseButton1Click doesn’t pass down any parameters, so player here will always be nil.
If you wanted to get the player still within the GUI, you could use this hacky function.

function GetPlayer()
	local parent = script.Parent 
	repeat
		parent = parent.Parent
	until parent == nil or parent == game or parent:IsA("Player")
	if parent and parent ~= game then
		return parent
	end
end

Also, I’d recommend that you generally use only one datastore key per player. The more keys you have, the longer loading and saving will take, which takes up more requests since you’re also limited on those, and it eventually all leads to data loss. Please, just keep your datastore limited to one big table in a singular key.

Thank you for the reply. Where should I put this in the script?

It’d work no matter where you put it, but I’d put it near the top right after you define the other variables.
Also just in case you need help on it, do this now at the top of the MouseButtonClick function

local player = GetPlayer()

is this right?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local StallDataStore = DataStoreService:GetDataStore("StallDataStore")

script.Parent.MouseButton1Click:Connect(function()
	local function GetPlayer()
		local parent = script.Parent 
		repeat
			parent = parent.Parent
		until parent == nil or parent == game or parent:IsA("Player")
		if parent and parent ~= game then
			return parent
		end
	end
	local StallSettings = GetPlayer().PlayerGui.Start.Settings.Stall
	local StallName = StallSettings.NameBox.TextBox
	local Timer = 0

	--Name Data Save
	if Timer == 0 then
		local success, errormessage = pcall(function()
			StallDataStore:SetAsync(GetPlayer().UserId.."-StallName",StallName.Text)

		end)

		if success then
			print("SavedName")
		else
			print("failed")
			print(errormessage)
		end

	else
		
		wait(1) 
		Timer = - 1 
		print(Timer)
	end

end)

It would work, but defining the function inside of another function each time is redundant. Put it up before the MouseButtonClick connection is made.
Also, the player still doesn’t get defined.

script.Parent.MouseButton1Click:Connect(function()
local player = GetPlayer()
-- blah blah blah

alright, its working know thank you!

Mark my post as the solution so others know the issue has been resolved :+1:

1 Like

thank you for helping me I think its working now

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