Help with a teleport script in my game

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I should preface this by stating that I have been away from ROBLOX developing for a good 3 years, and have left my one truly scripted-by-me game unfinished. With that said, I’m looking at it again and trying to untangle all of the spaghetti code that I put into it (I should also mention I have not been coding at all during this time). The game is a kind of turn-based game where everyone is on a grid of tiles and they all select a tile to jump to every few seconds, and different tiles have different power-ups, effects, attacks, etc. As you might already be able to tell there’s a lot of interconnectedness going on here and tracking down the source of bugs has become challenging fast. Long story short the very first tile effect I wanted to work on was a teleport tile where when you landed on it it would randomly teleport you to a different tile on the grid. Simple enough, right? Well, let me just paste the code and the output now and see if anybody can help me make heads or tails of this.

local bindable = game:GetService("ReplicatedStorage"):WaitForChild("bindable")
local Players = game:GetService("Players"):GetPlayers()
local playersPlayer

bindable.Event:Connect(function(player, tileToBeFired, Row)
	
	print('Teleport function fired!')
		
	if tileToBeFired == script.Parent.Name and tostring(Row) == script.Parent.Parent.Name then
		print("Teleportation sequence has started...")
		
		for i,v in pairs(Players) do
			print("Searching through available teleportation subjects...")
			if v.Name == player.Name then
				print("Eligible subject found...")
			playersPlayer = v
				return playersPlayer
			end
		end
	end
	
	print(playersPlayer.Name)
	playersPlayer.ClickRowString.Value = "ClickRow"..math.Random(1,5)
	print(playersPlayer.ClickRowString.Value)
	local rightHalf = (math.Random(1,2))
	if rightHalf == 1 then
		playersPlayer.ClickTileString.Value = "ClickTile00"..math.Random(1,9)
			
			
	else 
		playersPlayer.ClickTileString.Value = "ClickTile0"..math.Random(10,15)
	end
	
	for i,v in pairs(game.Workspace.TileGrid.ClickRows:GetChildren()) do
		if v.Name == playersPlayer.ClickRowString.value then
			for x,z in pairs(v:GetChildren()) do
				if z.Name == playersPlayer.ClickTileString.Value then
					playersPlayer.Character.cPart.Position = z.Position + Vector3.new(0, 3.5, 0)
				end
			end
		end
	end
	
	
				
	
	print("Player "..player.Name.." has telported!")
	
end)

Output:

19:46:55.206 Jumped! - Server - JumpP1:115
19:46:55.807 Teleport function fired! - Server - Script:7
19:46:55.807 Teleportation sequence has started… - Server - Script:10
19:46:55.807 Searching through available teleportation subjects… - Server - Script:13
19:46:55.807 Eligible subject found… - Server - Script:15
19:46:55.807 Teleport function fired! - Server - Script:7
19:46:55.807 Workspace.TileGrid.PeakRow2.Tile7.Script:22: attempt to index nil with ‘Name’ - Server - Script:22
19:46:55.807 Stack Begin - Studio
19:46:55.808 Script ‘Workspace.TileGrid.PeakRow2.Tile7.Script’, Line 22 - Studio - Script:22
19:46:55.808 Stack End - Studio
19:46:59.823 Click Detectors Added - Server - Main:269
19:46:59.857 Create Click Detectors Event Fired - Client - PlayerHandler:189
19:46:59.857 Value is not a valid member of Part “Workspace.TileGrid.Rows.Row6.Part” - Client - PlayerHandler:156
19:46:59.857 Stack Begin - Studio
19:46:59.857 Script ‘Workspace.GuruNin.PlayerHandler’, Line 156 - function lightTiles - Studio - PlayerHandler:156
19:46:59.858 Script ‘Workspace.GuruNin.PlayerHandler’, Line 191 - function onCreateClickDetectorsFired - Studio - PlayerHandler:191
19:46:59.858 Stack End - Studio
19:47:02.839 Click Detectors Destroyed - Server - Main:275
19:47:02.839 Jump command received… - Server - JumpP1:112
19:47:02.839 jumpTo fired! - Server - JumpP1:46
19:47:02.839 Player located at click row 4 - Server - JumpP1:50
19:47:02.839 Player is standing on click tile 2 - Server - JumpP1:53
19:47:02.840 ClickTile001 - Server - JumpP1:59
19:47:02.840 Jump command sent… - Server - Main:320
19:47:02.874 DestroyClickDetectors event fired - Client - PlayerHandler:196
19:47:02.874 Value is not a valid member of Part “Workspace.TileGrid.Rows.Row6.Part” - Client - PlayerHandler:180
19:47:02.874 Stack Begin - Studio
19:47:02.874 Script ‘Workspace.GuruNin.PlayerHandler’, Line 180 - function unlightTiles - Studio - PlayerHandler:180
19:47:02.874 Script ‘Workspace.GuruNin.PlayerHandler’, Line 198 - function onDestroyClickDetectorsFired - Studio - PlayerHandler:198
19:47:02.874 Stack End - Studio
19:47:03.772 Jumped! - Server - JumpP1:115
19:47:04.356 Orange! - Server - Script:5
19:47:04.356 Teleport function fired! - Server - Script:7
19:47:04.356 GuruNin - Server - Script:22
19:47:04.356 Workspace.TileGrid.PeakRow2.Tile7.Script:23: attempt to call a nil value - Server - Script:23
19:47:04.356 Stack Begin - Studio
19:47:04.356 Script ‘Workspace.TileGrid.PeakRow2.Tile7.Script’, Line 23 - Studio - Script:23
19:47:04.356 Stack End - Studio
19:47:04.357 Teleport function fired! - Server - Script:7
19:47:04.357 Workspace.TileGrid.PeakRow2.Tile7.Script:22: attempt to index nil with ‘Name’ - Server - Script:22
19:47:04.357 Stack Begin - Studio
19:47:04.357 Script ‘Workspace.TileGrid.PeakRow2.Tile7.Script’, Line 22 - Studio - Script:22
19:47:04.357 Stack End - Studio

OK a couple things about this output. It start’s with the print “Jumped!” which is a successful initiation of the script that carries the player from one tile to another. I land on the teleport tile, which you can see “Teleport function fired” then after a few prints because I was seeing if the issue was that it couldn’t find anybody to teleport, it once again says “Teleport function fired”. This is the first major issue I think may be going on, something is causing it to fire off multiple times and it’s possibly breaking as a result, maybe. Then after that, I get the error “Workspace.TileGrid.PeakRow2.Tile7.Script:22: attempt to index nil with ‘Name’ - Server - Script:22” referring to the line “print(playersPlayer.Name)”. This contradicts the print before it that said “Eligible subject found” which would mean that it successfully found my name and returned my player into the variable playersPlayer (pls don’t judge my variable naming from three years ago). Now, move past a bunch of prints and output and what-not and it gets to the second jump, with the print “Jumped!” immediately followed by “Orange!” (I think orange is referring to the color of tile? Not completely sure). After this, it prints my name, GuruNin, from the same script that called an error on it a moment ago. This is a problem for two reasons, one is that it should have successfully printed my name the FIRST time it ran, and second is that it shouldn’t have been fired at all this second time, because the teleport tile is supposed to run when you jump to it, not away from it. After this, I get the error “Workspace.TileGrid.PeakRow2.Tile7.Script:23: attempt to call a nil value - Server - Script:23”, referring to the line “playersPlayer.ClickRowString.Value = “ClickRow”…math.Random(1,5)”. The ClickRowString is a string that I automatically insert all of the players at the start of the game, and the scripts use it to determine which tile in the grid they’re going to jump to next. I have no idea why it calls this a nil value, unless playersPlayer is returning nil, which it isn’t, because it just printed my name when I put in print(playersPlayer.name) in the line above.

And you’ll notice that the teleport function fires AGAIN after this, and returns the same error. Firing 3 times for the price of one.

And also notice that it doesn’t print all of the “Teleportation subject found” stuff each time it fires, and it doesn’t attempt to print my name each time it fires, it’s like it’s selectively firing part of my script and not the other. I know that shouldn’t be possible, but here we are.

And since I haven’t mentioned it and it’s probably important, let me grab the script that actually calls this one to be fired…


local function fireTile()
	for i,v in pairs(plrs) do
		for x,z in pairs(rows) do
			if v.ClickRowString.Value == "ClickRow"..x then
				local Row = z
				for y,w in pairs(z:GetChildren()) do
					if v.ClickTileString.Value == "ClickTile00"..y then
						tileToBeFired = "Tile"..y
						player = v
						bindableFire:Fire(player, tileToBeFired, Row)
					elseif v.ClickTileString.Value == "ClickTile0"..y then
						tileToBeFired = "Tile"..y
						player = v
						bindableFire:Fire(player, tileToBeFired, Row)
					end
				end
			end
		end
	end
end


while true do
	wait(1)
	
	for i,v in pairs(plrs) do
		fireClientCreateClickDetectors(v)
	end
	
	wait(3)
	
	for i,v in pairs(plrs) do
		fireClientDestroyClickDetectors(v)
	end
	
	movePlayers()
	updateRows()
	wait(1)
	fireTile()
	wait(3)
end

It’s a lot of hubbub I know but the jist of it is that it’s locating the row and column that the player is currently located at and I think it’s running the script inside of that tile only? Somehow it’s firing that tile and not any other tile I forget how that works exactly but yeah. Then after that I just showed the sequence of functions that causes the game to run, it has a lot of functions that I didn’t show but they shouldn’t be relevant to this particular problem and it just shows the flow of how the game runs hopefully, it’s a real simple game despite all of the pain-in-the-butt scripting that goes into it haha. If anybody is nice enough to read this far and actually try and help I ask that you kind of explain stuff to me in a real basic way, like I said I haven’t touched coding in 3 years, but this is a passion project of mine because it’s based on a game that I played when I was younger that’s no longer available on the internet, so I’m not ready to give up on it yet. Thanks in advance, peace.

1 Like

Your code is returning instead of breaking the loop, which causes your function to stop running. You can also use :FindFirstChild instead of nested loops. Your code also says “Attempt to index nil with .Name” because your player variable is a string. Since BindableEvents can’t send over instances, it will attempt to convert it to a string. I have switched that out for sending over a userId.

Bindable Code:

local bindable = game:GetService("ReplicatedStorage"):WaitForChild("bindable")
local Players = game:GetService("Players")

bindable.Event:Connect(function(userId, tileToBeFired, Row)
	print('Teleport function fired!')

	if tileToBeFired ~= script.Parent.Name and tostring(Row) ~= script.Parent.Parent.Name then
		return
	end

	print("Teleportation sequence has started...")
	local player = Players:GetPlayerByUserId(userId)
	print(player.Name, "has been selected")
	
	player.ClickRowString.Value = "ClickRow"..math.random(1,5)
	
	local rightHalf = (math.random(1,2))
	
	if rightHalf == 1 then
		player.ClickTileString.Value = "ClickTile00"..math.random(1,9)
	else 
		player.ClickTileString.Value = "ClickTile0"..math.random(10,15)
	end
	
	local row = workspace.TileGrid.ClickRows:FindFirstChild(player.ClickRowString.Value)
	local part = row and row:FindFirstChild(player.ClickTileString.Value)

	if part then
		player.Character:MoveTo(part.Position)
	end

	print("Player "..player.Name.." has telported!")
end)

Other code:

local function fireTile()
	for i,v in pairs(plrs) do
		for x,z in pairs(rows) do
			if v.ClickRowString.Value == "ClickRow"..x then
				local Row = z
				for y,w in pairs(z:GetChildren()) do
					if v.ClickTileString.Value == "ClickTile00"..y then
						tileToBeFired = "Tile"..y
						player = v
						bindableFire:Fire(player.UserId, tileToBeFired, Row)
					elseif v.ClickTileString.Value == "ClickTile0"..y then
						tileToBeFired = "Tile"..y
						player = v
						bindableFire:Fire(player.UserId, tileToBeFired, Row)
					end
				end
			end
		end
	end
end
1 Like

Thanks so much for your response! I will implement it in the morning and let you know how it goes :slight_smile:

2 Likes

OK man here’s my feedback. Firstly, I love what you did with the first bit of code where it simply returns if the tile is NOT equal to the parameters sent by the bindable event, I don’t think I totally understood what return did when I was working on this before but now that I know it just passes the ball back to the script that sent it it makes total sense. I think that’s why I was also getting multiple “Teleportation function fired!” prints before, I had that print before it actually checked that there was indeed a player standing on the tile, so it would fire once for every teleport tile that happened to be in play.

With that said, (I also love the simplification you did to the rest of the script), it’s throwing an error in the output when it comes to the line “local player = Players:GetPlayerByUserId(userId)” and I’m not really understanding why. Here’s the error: “Unable to cast Instance to int64 - Server - Script:13”. Yeah, got no idea what that means. Also, if you could point me to a good resource for reading up all about roblox scripting, that would be greatly appreciated. I had one that was perfect that I used years ago but can’t seem to remember what it was or how to find it, it was sort of like a wiki that had in-depth explanations about basically any feature of lua or roblox scripting you could want.

1 Like

NVM, turns out I’m an idiot and forgot to paste the second part of the code you gave me into the main script. Thanks man, it works perfectly and you rock! I’m definitely going to be combing over the differences between your script and my script and see what I can learn and how I can improve my scripting going forward. This case is closed!

2 Likes

Roblox has a coding fundamentals course, which may be helpful for you to go over:

You should also do this other scripting tutorials by Roblox after you’re done the first one:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.