Trying to make teleport code

So I am coding a teleporter that teleports you to the players house. the teleporting works but it
I am trying to make a variable that says where it needs to teleport to.
This is the code I that wrote:

local tp1 = script.Parent.Parent.Parent.tp1
local owner = script.Parent.Parent.Parent:WaitForChild("Owner").Value 
local tp2 = workspace:FindFirstChild("%sHouseManager", owner):FindFirstChild("%sIndoorHouse", owner).tp2


function onTouch(part)
	if part.Parent.Humanoid ~=nil then
		part.Parent:MoveTo(tp2.Position)
		
	end
end
script.Parent.Touched:connect(onTouch)

It does not teleport me and I get this error:

Workspace.superfoose2HouseManager.House1Indoor.DoorOut.Teleporter.Script:5: Expected identifier, got ‘function’

Any help would be appreciated.

2 Likes

Which line is causing the error?

The error is on the word function. but I think it is on the teleport2 (tp2) variable.

Please show your whole Output window.

image

image

Instead of using :MoveTo do part.Parent.HumanoidRootPart.Position = tp2.Position

It still shows me this error.

image

1 Like

I think this is where 1 of your problems may lie? Line 2 can’t be defined like that if I recall correctly:

You could change that to:

local owner = script.Parent.Parent.Parent:WaitForChild("Owner")
local tp2 = workspace:FindFirstChild("%sHouseManager", owner.Value):FindFirstChild("%sIndoorHouse", owner.Value).tp2

Either or you may just need to define it as true or false as the API says

Also for the Touched event, could you make sure that the function inside the script is a BasePart of some sort?

1 Like

It shows the same error.

local tp1 = script.Parent.Parent.Parent.tp1
local owner = script.Parent.Parent.Parent:WaitForChild("Owner")
local tp2 = workspace:FindFirstChild("%sHouseManager", owner.Value):FindFirstChild("%sIndoorHouse", owner.Value).tp2

    function onTouch(part)
    	if part.Parent.Humanoid ~=nil then
    		--part.Parent:MoveTo(tp2.Position)
    		part.Parent.HumanoidRootPart.Position = tp2.Position
    		
    	end
    end
    script.Parent.Touched:connect(onTouch)

This is my current code.

2 Likes

Is Owner a StringValue or BoolValue?

1 Like

Owner is a string value for the code to work so it says who is the owner.

1 Like

The second parameter of Instance:FindFirstChild() is a bool, not a string.

1 Like

Refer to this again, the second parameter of FindFirstChild is a Type of Bool, not a String

So what do I need to do in order for this to work?

Why are you including the value of Owner in the FindFirstChild function?

Because the name of the model teleport2 is in is called the owners name and the model name.

Can you give an example of what the model is named?

1 Like

it can be called superfoose5HouseIndoor because of the string.format.

You are not using string.format() in your script.

local tp1 = script.Parent.Parent.Parent.tp1
local owner = script.Parent.Parent.Parent:WaitForChild("Owner")
local tp2 = workspace:FindFirstChild(string.format("%sHouseManager", owner.Value)):FindFirstChild(string.format("%sIndoorHouse", owner.Value)).tp2

function onTouch(part)
	if part.Parent.Humanoid ~=nil then
		--part.Parent:MoveTo(tp2.Position)
		part.Parent.HumanoidRootPart.Position = tp2.Position
		
	end
end
script.Parent.Touched:connect(onTouch)

Here now it has but the code tells me

Workspace.superfoose2HouseManager.House1.DoorIn.Teleporter.Script:3: attempt to index nil with ‘FindFirstChild’