I need to make a gui button,that will teleport you to the bossfight.But apparently,the line 6 gives an error: “attempt to index nil with ‘CFrame’”.I’ve never really encountered that before.
Anyways,here’s the script:
local part = workspace:FindFirstChild("TeleportPart")
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
if humanoidrootpart then
humanoidrootpart.CFrame = part.CFrame
end
end)
on local, some parts take a while to load, change FindFirstChild to WaitForChild:
local part = workspace:WaitForChild("TeleportPart")
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local humanoidrootpart = plr.Character:WaitForChild("HumanoidRootPart")
if humanoidrootpart then
humanoidrootpart.CFrame = part.CFrame
end
end)
local part = workspace:WaitForChild("TeleportPart")
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local humanoidrootpart = plr.Character:WaitForChild("HumanoidRootPart")
if humanoidrootpart then
if part then
humanoidrootpart.CFrame = part.CFrame
else
print("TeleportPart does not exist")
end
else
print("HumanoidRootPart Does not exist")
end
end)
It says,that the teleport part doesn’t exist if i put “FindFirstChild”,else, it just says,that “Infinite yield possible on ‘Workspace:WaitForChild(“TeleportPart”)’”.
local part = workspace:WaitForChild("TeleportPart",5)
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local humanoidrootpart = plr.Character:WaitForChild("HumanoidRootPart")
if humanoidrootpart then
if part then
humanoidrootpart.CFrame = part.CFrame
else
print("TeleportPart does not exist")
end
else
print("HumanoidRootPart Does not exist")
end
end)
task.wait(1.5)
local part = workspace:WaitForChild("TeleportPart")
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local humanoidrootpart = plr.Character:WaitForChild("HumanoidRootPart")
if humanoidrootpart then
if part then
humanoidrootpart.CFrame = part.CFrame
else
print("TeleportPart does not exist")
end
else
print("HumanoidRootPart Does not exist")
end
end)
make sure that its name is TeleportPart luau is case sensitive
you are getting that error because FFS and WFC will return nil if instance wasnot found and nil doesnot have CFrame