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

Tonumber still returning string?

I have code that will teleport a player to a coordinate they set in a textbox. I used tonumber() on each of the textboxes but when I type a number into them and click the button, the output says "nil"

I know that if you type anything else but a number into a tonumber() function, it will return nil. I don't know why this is happening. I'm typing a number into a textbox.Text with tonumber() on it but it's still returning nil.

Does anyone know how to fix this issue? Thanks!

Code:

local player = game.Players.LocalPlayer
local playergui = player.PlayerGui
local character = player.Character
local XCoordText = tonumber(playergui.ScreenGui.X.Text)
local YCoordText = tonumber(playergui.ScreenGui.Y.Text)
local ZCoordText = tonumber(playergui.ScreenGui.Z.Text)
local Teleport = script.Parent


Teleport.MouseButton1Click:Connect(function()
	if typeof(XCoordText) or typeof(YCoordText) or typeof(YCoordText) == nil then
		print(typeof(XCoordText, YCoordText, ZCoordText))
		script.Parent.Text = "Please type a correct value."
		wait(2)
		script.Parent.Text = "Button"
	else
		print(typeof(XCoordText, YCoordText, ZCoordText))
		character:MoveTo(Vector3.new(XCoordText, YCoordText, ZCoordText))

	end
end)

6 comments

0

what are xcoord,ycoord and zcoord when you print them?

0

also what are you trying to say with your if statement?

0

They say nil even when I type a number and my if statement is if the thing I type into them is nil print what type they are, else the type it is when it's you can teleport a player.

0

what is the text originally i mean

0

do print(":".. playergui.ScreenGui.X.Text..":")

0

It says this :123

1 answer

✓ Accepted answer

Try doing this (do not use tonumber before this function, because 1: the text before the function fires is always the same no matter what happens, 2: it will stay nil if the text before the function isn't a number):

local player = game.Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local character = player.Character or player.CharacterAdded:Wait()
local XCoordText = PlayerGui:WaitForChild("ScreenGui").X.Text
local YCoordText = PlayerGui:WaitForChild("ScreenGui").Y.Text
local ZCoordText = PlayerGui:WaitForChild("ScreenGui").Z.Text
local Teleport = script.Parent

Teleport.MouseButton1Click:Connect(function()
	XCoordText = string.match(PlayerGui:WaitForChild("ScreenGui").X.Text, "[%d%p]+")
	YCoordText = string.match(PlayerGui:WaitForChild("ScreenGui").Y.Text, "[%d%p]+")
	ZCoordText = string.match(PlayerGui:WaitForChild("ScreenGui").Z.Text, "[%d%p]+")

	XCoordText = tonumber(XCoordText)
	YCoordText = tonumber(YCoordText)
	ZCoordText = tonumber(ZCoordText)

	if typeof(XCoordText) == nil or typeof(YCoordText) == nil or typeof(ZCoordText) == nil then -- fixed the "ZCoordText" error here where  you typed two "YCoordText" checks.
		script.Parent.Text = "Please type a correct value."
		wait(2)
		script.Parent.Text = "Button"
	else
		character:MoveTo(Vector3.new(XCoordText, YCoordText, ZCoordText))
	end
end)

Hope this helps!

1 comment

0

Thank you so much!

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