Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Teleporting not working properly or something?

I just tried to make a brick that teleports you back to a position of (5, 5, 5), I tested in Play Solo mode and it works for around five time but then after that it doesn't work anymore. It gave me this error:

17:09:46.732 - Workspace.Part.Script:11: attempt to index global 'player' (a nil value) 17:09:46.733 - Stack Begin 17:09:46.734 - Script 'Workspace.Part.Script', Line 11 17:09:46.737 - Stack End

And here is my script

local isOn = false

target = CFrame.new(5, 5, 5)


function teleport(hit)
	if not isOn then
		isOn = true
	name = hit.Parent.Name
	player = game.Workspace:FindFirstChild(name)
	player.Torso.CFrame = target
	isOn = false
 end
end

script.Parent.Touched:connect(teleport)

1 answer

✓ Accepted answer

It's possible that something entirely unrelated to a character is touching the brick, especially if it's unanchored. If said object is in Workspace directly, and not part of a model, the script can't find it's parent in Workspace because it IS Workspace. It's like looking for a box inside itself.

I suggest adding an if statement to make sure that what it's finding actually exists, and that it is in fact a character. Here's an example:

local isOn = false

target = CFrame.new(5, 5, 5)

function teleport(hit)
	if not isOn then
		isOn = true
		local name = hit.Parent.Name
		if Workspace:findFirstChild(name) then
 			local player = Workspace:findFirstChild(name)
			if player:findFirstChild("Humanoid") and player:findFirstChild("Torso") then
 				player.Torso.CFrame = target
			end
		end
 		isOn = false
	end
end

script.Parent.Touched:connect(teleport)

1 comment

0

Ok, thanks. It works now

This discussion is preserved as it was. New answers, comments, and votes are closed.