I'm making a part that will teleport a player to a game within my place's Universe. When a person touches the part, instead of teleporting, their game freezes, and they are forced to close ROBLOX. Here's the teleport script, which is inside a part in the Workspace:

Allowed_Players = {"Minifig77", "bramblepath2"} --Whitelist

db = false
script.Parent.Touched:connect(function(part)
	if db == false then
		db = true
		local plr = game.Players:GetPlayerFromCharacter(part.Parent)
		if plr then
			for i,v in pairs(Allowed_Players) do --Iterate over whitelist
				if v == plr.Name then --Check for permission
					game:GetService("TeleportService"):Teleport(431505384, plr) --Teleport
					wait(2)
					db = false
					return
				end
			end
		else
			wait(1)
			db = false
		end
	end
end)

The same thing happens in a portal I scripted:

Whitelist = {16754553, 8184187, 26275462} --User IDs of allowed players
db2 = false
mode = 1
--The code below was cut out of a larger function.
con = script.Parent.Touched:connect(function(part)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
	if plr and db2 == false then
		--Teleport
		db2 = true
		if mode == 1 then --Mode 1 means to teleport to a certain place...
			game:GetService("TeleportService"):Teleport(Places[mode], plr) --Teleport
		elseif mode == 2 then --While mode 2 takes the player to another place.
			for i,v in pairs(Whitelist) do --Iterate over Whitelist
				if plr.UserId == v then --Check for permissions
				game:GetService("TeleportService"):Teleport(Places[mode], plr) --Teleport
			end
		end
		end
		wait(.1)
		db2 = false
	end
end)

However, when I made a simpler version of the script for a place that demonstrated the problem, the teleport worked seamlessly.

This script also worked fine in the same place as my previous two scripts:

game.Players.PlayerAdded:connect(function(plr)
	wait(10)
	game:GetService("TeleportService"):Teleport(431505384, plr)
end)

Why do the first two scripts crash the player, while the third works fine? Is there any way I can make them work?

Thank you for your time.

3 comments

0

You should check that the hit part has a humanoid before trying to get the player from it.

0

Any errors?

0

I added a check for humanoids, but I still crashed. No errors in the output (dev console) when someone else tried it. (They still crashed, though.)