Why is my script indexing the character with nil?

In a teleportation script using a proximity prompt, my script is indexing the character with nil when you try to trigger the prompt and you don’t get teleported. Here is my script:

local prompt = game.Workspace.EnterBuildingPrompt.ProximityPrompt
local telePos = Vector3.new(-155.271, -4.239, -39.476)

function PromptActivated()
	local plr = game.Players.LocalPlayer
	local char = plr.Character
	char:MoveTo(telePos)
end

prompt.Triggered:Connect(PromptActivated)

And the output:

  13:26:33.221  Workspace.EnterBuildingPrompt.ProximityPrompt.EnterBuilding:6: attempt to index nil with 'Character'  -  Server - EnterBuilding:6

Anyone know the problem?

Well first off you should be using this guide when you are using proximity prompts. Also right now you are attempting to move the character NOT using the humanoid. To keep it the way you have it you would do this below.

local prompt = game.Workspace.EnterBuildingPrompt.ProximityPrompt
local telePos = Vector3.new(-155.271, -4.239, -39.476)

function PromptActivated()
	local plr = game.Players.LocalPlayer
	local char = plr.Character:WaitForChild("Humanoid")
	char:MoveTo(telePos)
end

prompt.Triggered:Connect(PromptActivated)

but a better way to run this would be to do something like this:

local prompt = game.Workspace.EnterBuildingPrompt.ProximityPrompt
local telePos = Vector3.new(-155.271, -4.239, -39.476)

function PromptActivated()
	local plr = game.Players.LocalPlayer
	local char = plr.Character:WaitForChild("HumanoidRootPart")
	char.CFrame = CFrame.new(-155.271, -4.239, -39.476)
end

prompt.Triggered:Connect(PromptActivated)

Alright, I’ll try that.

Characters

Still the same output:

13:37:20.478  Workspace.EnterBuildingPrompt.ProximityPrompt.EnterBuilding:6: attempt to index nil with 'Character'  -  Server - EnterBuilding:6

Where is this script par say. Because if this is in a client sided than it will work server sided not a chance and you would have to do this:

local ProximityPromptService = game:GetService("ProximityPromptService")
local prompt = game.Workspace.EnterBuildingPrompt.ProximityPrompt
local telePos = Vector3.new(-155.271, -4.239, -39.476)

local function onPromptTriggered(promptObject, player)
    if promptObject == prompt then
        local char = game.Workspace:FindFirstChild(player.Name):WaitForChild("HumanoidRootPart")
        char.CFrame = CFrame.new(-155.271, -4.239, -39.476)
    end
end

ProximityPromptService.PromptTriggered:Connect(onPromptTriggered)
1 Like

So it has to be a local script?

Yeah, make it a local script and it should work fine.

doesn’t have to be but the script ur using would only work client sided but I suggest doing the server sided script I sent.