I have a script that allows the player to view houses and teleport to them.
I want to make it where the player cannot teleport if they are sitting because then the vehicle will teleport with them. I keep getting an error at line 76 saying: “attempt to index nil with ‘sit’”.
It is also printing the humanoid as nil.
Code:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Human = char:FindFirstChildWhichIsA("Humanoid")
print(char)
print(Human)
local cam = workspace.CurrentCamera
local Label = script.Parent.TextLabel
local HouseTP = game.ReplicatedStorage.HouseTP
local Button = script.Parent.TextButton
local HouseNum = "House 1"
local Warn = game.StarterGui.ScreenGuis.WarningFrame.TextLabel
local function alphabeticalOrder(instance)
local children = instance:GetChildren()
table.sort(children, function(c1, c2)
return tonumber(c1.Name) < tonumber(c2.Name)
end)
return children
end
local camParts = alphabeticalOrder(workspace.Cameras)
local LArrow = script.Parent.LeftArrow
local RArrow = script.Parent.RightArrow
local GUI = script.Parent.Parent.ButtonFrame.Houses
local arrowClickCooldown = false
GUI.MouseButton1Click:Connect(function()
if script.Parent.Visible == false then
LArrow.Visible = true
RArrow.Visible = true
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = camParts[1].CFrame
local camNumber = 1
RArrow.MouseButton1Click:Connect(function()
if arrowClickCooldown then return end
arrowClickCooldown = true
if camNumber == #camParts then
cam.CFrame = camParts[1].CFrame
camNumber = 1
HouseNum = "House "..tostring(camNumber)
Label.Text = HouseNum
else
cam.CFrame = camParts[camNumber + 1].CFrame
camNumber = camNumber + 1
HouseNum = "House "..tostring(camNumber)
Label.Text = HouseNum
end
wait(0.1)
arrowClickCooldown = false
end)
LArrow.MouseButton1Click:Connect(function()
if arrowClickCooldown then return end
arrowClickCooldown = true
if camNumber == 1 then
cam.CFrame = camParts[#camParts].CFrame
camNumber = #camParts
HouseNum = "House "..tostring(camNumber)
Label.Text = HouseNum
else
cam.CFrame = camParts[camNumber - 1].CFrame
camNumber = camNumber - 1
HouseNum = "House "..tostring(camNumber)
Label.Text = HouseNum
end
wait(0.1)
arrowClickCooldown = false
end)
else
cam.CameraType = Enum.CameraType.Custom
script.Parent.LeftArrow.Visible = false
script.Parent.RightArrow.Visible = false
end
end)
Button.MouseButton1Click:Connect(function()
if Human.Sit == false then
cam.CameraType = Enum.CameraType.Custom
script.Parent.LeftArrow.Parent.Visible = false
HouseTP:FireServer(HouseNum)
else
Warn.Text = "You must be standing!"
Warn.Visible = true
wait(3)
Warn.Visible = false
Warn.Text = ""
end
end)