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

Help Teleporting a Player?

So this goes along with a question that I've previously asked (which has been answered). Here's the script again:

wait(1)
character = game.Players:findFirstChild(""..game.Workspace.ValueHold.Player.Value.."")


local t = game.Workspace.ValueHold.RoomCount

t.Changed:connect(function()
	print("Changing Room...")
	local r = math.random(1,3)
	if r == 1 then
		local room = game.ServerStorage.defroom1:Clone()
		room.Parent = game.Workspace
		game.Workspace.ValueHold.CurrentRoom.Value = 1
		character.Character.Torso.CFrame = CFrame.new(game.Workspace.defroom1.spawn.Position)
		elseif r == 2 then
		local room2 = game.ServerStorage.defroom2:Clone()
		room2.Parent = game.Workspace
		game.Workspace.ValueHold.CurrentRoom.Value = 2
		character.Character.Torso.CFrame = CFrame.new(game.Workspace.defroom2.spawn.Position)
		elseif r == 3 then
		local room3 = game.ServerStorage.defroom3:Clone()
		room3.Parent = game.Workspace
		game.Workspace.ValueHold.CurrentRoom.Value = 3
		character.Character.Torso.CFrame = CFrame.new(game.Workspace.defroom3.spawn.Position)
	end
end)

Now, time for me to explain. So, you touch a button, you get teleported to a new room, that room has a button that does the same thing, yadda yadda yadda, nothing important, until you get to the actual teleporting part. So, I have a value that stores the players name upon entering the game. So, if your name is "awesomedude123", then the value changes to "awesomedude123". The beginning of the script defines "character" as "awesomedude123", and when a random room is picked, it takes "character" and teleports him to a new room. Same story as my last problem: Works In test mode, but not in a server. I've been told to add a "wait(1)' before it, and that should fix the problem, but it doesn't.

All help is greatly appreciated.

3 comments

0

Is this a localscript?

0

Is this single player?

0

Add a print("Hello World!") after the character variable. Does the server print Hello World! or not in the output window?

1 answer

I recommend using a LocalScript for this.

Turn on FilteringEnabled, and insert a LocalScript into StarterPlayer.StarterPlayerScripts.

This way, you don't need to mess around with values or anything to find out which player to teleport.

Your script might look something like this

repeat wait() until game.Players.LocalPlayer;
local player = game.Players.LocalPlayer
local character = player:WaitForChild("Character")

local rooms = {game.ServerStorage.defroom1, game.ServerStorage.defroom2, game.ServerStorage.defroom3}
local buttons = {game.Workspace.button1, game.Workspace.button2, game.Workspace.button3}
local currentRoom = {} --The script will put stuff in here

local jammed = false --This is for debounce. If a player touches the button, we only want the script to register it once.

-----
local seeder = coroutine.create( function() while true do math.randomseed(game.Workspace.Randomparts.a1.Rotation.X or tick()) coroutine.yield() end end)
---- This creates seeds for random numbers so it creates a different random sequence each time!

function createRoom(Index)
	local newRoom = rooms[Index]:Clone()
	table.insert(currentRoom,newRoom)
	newRoom.Parent = workspace
	local button = newRoom:FindFirstChild("Button", true)
	button.Touched:connect(buttonTouched)
end

function buttonTouched() --Create a function for each button that fires when the button is touched
	if not jammed then
		jammed = true --make sure no more functions can be created
		
		coroutine.resume(seeder)
		local newRoomIndex = math.random(1,#rooms) --pick a random room (could be the same one)
		print("Changing Room...")
			
		--Remove old room
		currentRoom[1]:Destroy()
		table.remove(currentRoom,1)
			
			
		--Create new room
		createRoom(newRoomIndex)
			
		character.Torso.CFrame = CFrame.new(rooms[newRoomIndex].spawn.Position) --Teleport player
			
		jammed = false --make the sript able to run again
	end
end
--Setup--
createRoom(1)

3 comments

0

Make sense, however the buttons aren't a part of workspace. Their an Unnamed door inside of each individual room. I could rename the doors, however what would I do to target them?

0

Ok, so I edited the script to find the correct location of the buttons, however when the button is touched, the room just disappears. I don't get teleported. Help?

0

I edited my script to work how you want it to. My script assumes there is only one button in each room, and that it is named "Button", and is the only thing named "Button". Please hit "Accept Answer" if my answer is sufficient.

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