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

Input from GUI changing information in tool?

Hi, so I am kinda new to Lua and have been experimenting with several things. I was wondering if it's possible for me to get the input from a textbox GUI and from that input, use that to change information in a tools scripts. An example can be the player setting how much damage the gun will do manually. So they input 15, that input changes the tools damage in the script to 15. Another example can be the user setting their own gun cursor. Mouse.icon doesnt work as the gun already has a default cursor set.

1 answer

In the script that describes the damage, you can use TextBox.FocusLost to determine whether the player pressed enter and to change the damage

Like,

local damage = 10 -- lets say the default damage is 10
local TextBox = -- where is the textbox located?

TextBox.FocusLost:Connect(function(enterPressed)
	if enterPressed then -- if they pressed enter to lose focus on the textbox
		damage = TextBox.Text -- change damage
	end
end)

https://developer.roblox.com/en-us/api-reference/event/TextBox/FocusLost You don't have to do this but you can add a pcall to determine if the player only put numbers and no letters https://devforum.roblox.com/t/pcalls-when-and-how-to-use-them/393687

local damage = 10
local TextBox = -- textbox location

TextBox.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		local success,errormsg = pcall(function() -- success = it worked, error = it failed
			damage = tonumber(TextBox.Text) -- convert to number
		end)
		if success then -- if it succeeded
			print("there were no letters") -- optional, you can return nil as well
		else -- there was a problem with converting to number
			print("error setting damage value")
			warn(errormsg) -- warns in the output the error message that came with the conversion
		end
	end
end)

And for changing the mouse, I do not know how to do that but I'm guessing they would put the rbxassetid in the input, and then applying it with the id of the image asset. https://developer.roblox.com/en-us/api-reference/class/Mouse

Hope this helps

1 comment

1

The function doesn't error as long as you pass strings to it, which is always the case here. If it contains any character other than a digit, the function returns nil. Technically, there's no error that occurred, so pcall is redundant, rather you can do a simple check: damage = tonumber(TextBox.Text) or 20. 20 would be the default in the case where the user entered a character that isn't a digit

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