Error in my code

I am currently having trouble with this code, is anyone able to help?

**local TeleportService = game:GetService(“TeleportService”)
local gameID = game.teleportblock.GameIDValue.Value

script.Parent.MouseButton1Click:Connect(function()
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
TeleportService:Teleport(gameID, player)
end
end)**

You haven’t defined the hit variable, meaning that when you call it with GetPlayerFromCharacter, the script has no clue what you are referring to. All you have to do is include it in the parenthesis following function.

It’s a MouseButton1Click event, why would hit be passed as a parameter?

2 Likes

I just realised this after I posted it - I changed it from a teleporter block - what would I change it to?

If its Gui and Local, Do

game.Players.LocalPlayer
2 Likes

You can’t get the player from a MouseButton1Click event, unfortunately. You would have to use a LocalScript to detect the click and then FireServer with a RemoteEvent. Or you could just teleport with a LocalScript and just use the LocalPlayer, like Wizard said.

1 Like

I misread and thought it was using a clickdetector, my bad.

So, assuming it is a gui use LocalPlayer. If it is a surface gui you will get some other issues, as you would have to set the surfacegui’s adornee to whatever part you want it to be on and parent the surfacegui to the startergui for it to work properly.

1 Like

I’ll try that now, thank you. [30 chars]

Regarding this, I got this error:

First, there is some problems

Ok, it looks like a SurfaceGui, your using.

There might be no parent of game.TeleportBlock.

--[[
workspace.teleportblock or game.Workspace.teleportblock
--]]

If your using a local script use this:

local TpService = game:GetService("TeleportService")
local gameId = workspace.teleportblock.GameIDValue.Value

script.Parent.MouseButton1Click:Connect(function()
	TpService:Teleport(game.Players.LocalPlayer, gameId)
end)
2 Likes

Oh, you’re doing game.teleportblock. Where is the teleportblock? I’d assume it’s under workspace, but it’s definitely not under game.

3 Likes

I agree on that one, since its in the workspace.

2 Likes

I didn’t notice this error until then, that’s just a quick mistake. Thanks for the help, bacion, I’ll try your code now.

I might make some mistakes so see the edited code.

This seems to be the only error right now:

Where it says

TpService:Teleport(game.Players.LocalPlayer, gameId)

Switch game.Players.LocalPlayer and gameId around.

I changed that, yet it still comes up with this:

I apologize for the amount of replies!

What is gameID? I believe that workspace.teleportblock.GameIDValue.Value would be it. Check if there’s actually something there.

1 Like

There is something there, I know for sure as it functioned correctly with that value when I used a teleporter block.

image

Ah, I see what the issue is. It was passed as a string and not a number.

So you have two choices to fix: you can convert the StringValue to a NumberValue or you can use tonumber

TpService:Teleport(tonumber(gameId), game.Players.LocalPlayer)
1 Like