Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

"Unable to cast string to int" error - what?

Asked by 9 years ago

So I'm working on a GUI that teleports you to games I've made, although an "Unable to cast string to int" error always comes up in the output when I edit a particular line in my code:

function onClicked(mouse)
    print("Teleport Script Loaded")
    wait (1)
    game:FindService("TeleportService"):Teleport("id")
    wait (1)
    print ("Teleport Script Successful")
    wait (1)
end

script.Parent.MouseButton1Click:connect(onClicked)

I know it's something to do with line 4 because it manages to print "Teleport Script Loaded" but never gets past that, also it says that there's an issue with line 4 in the output.

Any ideas on what's going on?

1
Remove the quotations around 'id' DigitalVeer 1473 — 9y
0
I would suggest picking an answer to mark as an answer. iaz3 190 — 9y

4 answers

Log in to vote
2
Answered by 9 years ago
local id = 0 --Put correct place id here
function onClicked(mouse)
    print("Teleport Script Loaded")
    wait (1)
    game:FindService("TeleportService"):Teleport(id)
    wait (1)
    print ("Teleport Script Successful")
    wait (1)
end

script.Parent.MouseButton1Click:connect(onClicked)

All you had to do was remove the quotes around 'id' and make sure id had an int value (round number)

~ DigitalVeer already posted this answer as a comment but this way people will see the question is already answered from the site.

Ad
Log in to vote
3
Answered by
iaz3 190
9 years ago

Remove the quotes around id.

It is expecting a number, you are providing a string. If ID is a variable, it is also not being used. the literal string, "id" is.

As you can see here Teleport, from TeleportService, expects an int.

What the error is saying is that it is unable to use the string, and an int(a number) is expected.

Differences:

-- This is a number. it is valid.
number = 12345
-- This is not a number. It contains characters other than 0-9 or math functions (such as "*", "/", "+", "-")
notANumber = 1234a
-- This is a string. It is valid. It is different from number. it can be converted to a number by using tonumber(str)
str = "12345"
-- This is still a string. It can not be converted into a number using tonumber() because It contains characters other than 0-9 or math functions
stillAString = "1234a"
Log in to vote
0
Answered by
yumtaste 476 Moderation Voter
9 years ago

That error message means you are trying to use a string value for something that requires a number.

In your case, you tried to teleport a player to a place whose ID is "id," instead of something like 123456789. Just remove the quotes around the "id" on line 4.

Log in to vote
0
Answered by 9 years ago

Thanks for the answers, guys. But I forgot to say that there was a number value in the TextButton that I was using to launch the game, in which buttons that display details of the game in a GUI would also change the value to the place Id.

This is what I ended up with, if anyone wants to use it for their own game:

id = script.Parent.PlaceId.Value

function onClicked(mouse)
    print("Teleport Script Loaded")
    wait (1)
    game:FindService("TeleportService"):Teleport(id)
    wait (1)
    print ("Teleport Script Successful")
    wait (1)
end

script.Parent.MouseButton1Click:connect(onClicked)

Thanks again for the help!

0
BTW is this a local script? EzraNehemiah_TF2 3552 — 9y

Answer this question