I’m pretty new to scripting, I hope this is not a dumb mistake of mine. I wanted to make a lobby where the players touch a part and are automatically teleported to a vehicles seat. The script seems to have a problem detecting the player or something, but I don’t know how to solve or where the problem is. Could someone help me out?
The error is down below. Please say to me if I need to put more information!
Output:
10:32:35.789 - Workspace.JoinCar.Script:12: attempt to index nil with ‘CFrame’
It is not recommended to teleport the player to a seat instead use :Sit(Humanoid), so your code should look something like this:
local Hum = hit.Parent:FindFirstChild(“Humanoid”)
if not (Hum) then return end -- If no humanoid exists
workspace.Car.Seat:Sit(Hum)
The reason why you are getting the error: attempt to index nil with ‘CFrame’ is becasue Humanoid’s do not have a CFrame, if you want to teleport a player / character you should use the HumanoidRootPart. So that code should look something like this:
local HRP = workspace[Name].HumanoidRootPart
HRP.CFrame = CFrame.new(Pos, Look)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(Postion)
end
Humanoid is an object, that makes a players character look like real, and has some properties such as Health, Walkspeed and etc. You can learn about it here Humanoid
If you want to teleport a players character, you can use hit.Parent:SetPrimaryPartCFrame(location)
or hit.Parent.HumanoidRootPart.CFrame to do so.
In addition to both above posts in general when using coordinates to move/position objects, you can only use part based objects. Like a normal part, union, MeshPart e.t.c.
There are two main types of coordinated positioning in roblox physically, this is Vector3 and CFrame
There is not much difference between the two other than that CFrame gives a wide variety of options such as angles, toworldspace, interpolation e.t.c.
When moving and positioning parts you might recieve an error saying Vector3 value expected got CFrame or the other way around. Some parts or methods require a CFrame or Vector3 counterpart in order to function correctly.
To summarise:
New Vector3 coordinate: Vector3.new(x,y,z)
New CFrame coordinate: CFrame.new(x,y,z)
Default Vector3 value: Part.Position
Default CFrame value: Part.CFrame