LocalScript fails with no error, no visible mistakes in code

I am writing a script that will detect when a button is clicked, then will disable the parent screengui and teleport the player to the position of a part in the workspace.

I was facing 2 errors previously,

TeleportPart is not a valid member of Workspace “Workspace"
and
infinite yield possible on WaitForChild:(“TeleportPart”) --When I used this

After an hour of troubleshooting, I have not found the issue with my code.

Here is my localscript:


local button = script.Parent
local mainMenu = button.Parent

button.Interactable = true

task.wait(.001)

local player = game.Players.LocalPlayer
local teleportPart 
repeat
	teleportPart = game.Workspace:FindFirstChild("TeleportPart")
	task.wait()
until teleportPart

button.MouseButton1Click:Connect(function()
	print("Button Clicked")
	button.Interactable = false
	local character = player.Character
	local humanoidRootPart = character.WaitForChild("HumanoidRootPart")
	humanoidRootPart.CFrame = teleportPart.CFrame
	mainMenu.Enabled = false
end)

here is my explorer:


I would appreciate some support with this and I can answer any extra questions you may have

2 Likes

I suspect that it is getting stuck at the repeat until bit. Just to test, can you please go to the properties of workspace and turn off Streaming (StreamingEnabled)?

(Also, don’t worry about the ‘Infinite yield’ thing - it’s a warning, not an error. You can safely ignore it and it will proceed from that point in the script once the object exists)

1 Like

First, use this.

local teleportPart = workspace:WaitForChild("TeleportPart")

Second, disable StreamingEnabled on workspace.

Instance methods require the self argument to be passed, whether implicitly (via __namecall using :) or explicitly.
(using : is preferred, refer to Luau Performance if you want the technical details)

You code will throw an “Expected ‘:’ not ‘.’ calling member function WaitForChild” error.

This is the output I get when I turn off StreamingEnabled.

Thank you! This along with turning off StreamingEnabled fixed it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.