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

I need help with a teleportation script thing!?

I want to make it so when a player touches a certain part they can type for example: teleport 1 and the would be teleported to another place in my universe, here is the error I am getting: Workspace.Model.TeleportPad.Script:8: attempt to index field 'Chatted' (a nil value)

Here is my script so far:

local TeleportService = game:GetService("TeleportService")
local floor1Id = 518904899
local floor2Id = 518905949

script.Parent.Touched:connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		local player = hit.Parent:findFirstChild("Humanoid").Parent.Name
		player.Chatted:connect(function(msg)
			if msg:sub(1,8) == "teleport" then
				if msg:sub(10) == 1 then
					TeleportService:Teleport(floor1Id, player)
				elseif msg:sub(10) == 2 then
					TeleportService:Teleport(floor2Id, player)
				end
			end
		end)
	end
end)

2 comments

0

can you please put this in a code block.

0

Yeah

1 answer

You wrote this? Looks like a Free Model Script to me, but OK.

local player = hit.Parent:findFirstChild("Humanoid").Parent.Name -- Are you serious?

HERE IS HOW IT SHOULD BE DONE

-- ServerScript.

local PLAYERS = game:GetService("Players")
local TS = game:GetService("TeleportService")
local Part = script.Parent
local ID_1 = 518904899
local ID_2 = 518905949
local Table = {}

PLAYERS.PlayerRemoving:connect(function(Plr)
	if Table[Plr.UserId] then
		Table[Plr.UserId] = nil -- Remove the Debounce.
	end
end)

Part.Touched:connect(function(Hit)
	local Plr = PLAYERS:GetPlayerFromCharacter(Hit.Parent)

	if Plr then
		if not Table[Plr.UserId] then -- Could also do only Plr, but I prefer using the UserId.
			Table[Plr.UserId] = true
			
			Plr.Chatted:connect(function(Msg)
				-- The rest you can of course fill in yourself!!!
			end)
		end
	end
end)

I wrote this pretty quickly, so contact me if I made an error.

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