Hello. I am a new Roblox dev trying to learn how to code. My knowledge is very limited, but I do know enough to make the script you see a bit further down. My goal is to create a part that will teleport you to the other side of it when you touch it, but for some reason it only partly works.
Here’s the problem. When I first touch the part, it works as it should. but when I touch it a second time, I get this error message:
Workspace.CoolPart.Script:24: attempt to index nil with ‘Position’
After looking it up, I now think I know what this error message means, but I still don’t know how to solve it. The reason why I am so confused is because it worked once, but when I try again it doesn’t work. And before anyone asks, the code does work when I try it a third time. and then when I try a forth time it doesn’t work.
Here’s the script:
local part = script.Parent
local monkey = true
local coolDown = true
part.Touched:Connect(function(otherPart)
local humaniod = otherPart.Parent:FindFirstChild("Humanoid")
local RootPart = otherPart.Parent:FindFirstChild("HumanoidRootPart")
if humaniod then
if coolDown == true then
if monkey == true then
monkey = false
coolDown = false
RootPart.Position = RootPart.Position + Vector3.new(0, 0, 10)
wait(1)
coolDown = true
end
end
else
if coolDown == true then
monkey = true
RootPart.Position = RootPart.Position + Vector3.new(0, 0, -10)
wait(1)
coolDown = true
end
end
end)
So, does anyone know how to fix this?