Teleport script not working

I attempted to make a teleport script that would teleport any player with said badge to another place upon touch, and it does not appear to be working. I get the error message “Workspace.Door.Script:3: attempt to index nil with ‘Parent’” in terminal. Any ideas?

badgeId = 2124634329 -- Some random badge I own
local TeleportService = game:GetService("TeleportService")
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Place = 3233893879 -- Game with the badge

function badgeVip(hit)
	local h = hit.Parent:findFirstChild("Humanoid")
	if h ~= nil then
		local plyr = game.Players:GetPlayerFromCharacter(hit.Parent)
		badge = game:GetService("BadgeService")
		if plyr ~= nil then
			if badge:UserHasBadge(plyr.userId, badgeId) then
				TeleportService:Teleport(Place, player)
			else 
                        h.Health = 0 end
		end
	end
end

script.Parent.Touched:connect(badgeVip)

The ‘hit’ variable does not seem to be defined, does it exist?
If it doesn’t exist then there’s no way you’re going to get It’s parent.

You should just delete it, as it doesn’t seem that you’re using that ‘player’ variable.

1 Like

“hit” is not defined on line 3. Although I don’t think you even need that line anyway since you are using “plyr” instead. You would just need to replace “player” with “plyr” in the Teleport method too.

Ah, didn’t notice that. Do you know how to properly define it so that the script works?

Hello!
You are trying to define the touching player before he actually touches!
So youu should remove this line: local player = game.Players:GetPlayerFromCharacter(hit.Parent) (it’s the 3rd line of your code).
Also when I look on your script I see you are using game.Players. It is not really a good practice. Please consider changing it to game:GetService("Players").

Have a nice rest of your day!

1 Like

You should just delete it, doesn’t seem that you need to use that ‘player’ variable at all, instead at:

Just use the ‘plyr’ variable you’ve created.

Thank you, the script now works perfectly. The other comment about “Players” was very helpful, too.

No problem! I’m glad I was able to resolve your problem.

1 Like