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

Simple script isnt teleporting players to a part?

I have a script and its inside a button, the buttons inside a frame inside StarterGUI.

When you click the button its supposed to teleport you to a map that is stored inside Workspace inside a folder called MapStorage. The map has children that are spawns.

Heres the script, I have 2 version that I tried. I tried one that fired an event because I thought it may be a server - client error so I tried using a RemoteEvent but that didnt work. So I just put another version of the script directly under the button. Here are both of the scripts.

This is the script inside the button

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer


script.Parent.MouseButton1Click:connect(function()
	print("So it is firing")
	local ChoseMap = game.Workspace.MapStorage:FindFirstChildOfClass("Model")
	local RedSpawns = ChoseMap.Spawns:WaitForChild("RedSpawns"):GetChildren()
	local BlueSpawns = ChoseMap.Spawns:WaitForChild("BlueSpawns"):GetChildren()
	print("It found the spawns")
	if player.Team.Name == "Really red" then
		if player and #RedSpawns > 0 then
			print("We got this far!")
			local UpperTorso = player.Character:WaitForChild("HumanoidRootPart")
			local AllSpawns = math.random(1, #RedSpawns)
			local RandomSpawn = RedSpawns[AllSpawns]
			if RandomSpawn and UpperTorso then
				table.remove(RedSpawns, AllSpawns)
				UpperTorso.CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
			end
		end
	elseif player.Team.Name == "Really blue" then
		if player and #BlueSpawns > 0 then
			print("We got this far!")
			local UpperTorso = player.Character:WaitForChild("HumanoidRootPart")
			local AllSpawns = math.random(1, #BlueSpawns)
			local RandomSpawn = BlueSpawns[AllSpawns]
			if RandomSpawn and UpperTorso then
				table.remove(BlueSpawns, AllSpawns)
				UpperTorso.CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
			end
		end
	end	
	script.Parent.Modal = false
	script.Parent.Parent.Parent.Enabled = false
end)

That script prints "It found the spawns" but it doesnt print anything else, nor does it error.

Heres the RemoteEvent script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportPlayer = ReplicatedStorage:WaitForChild("TeleportPlayer")

local function onTeleportPlayerFired(player)
	print("So it is firing")
	local ChoseMap = game.Workspace.MapStorage:FindFirstChildOfClass("Model")
	local RedSpawns = ChoseMap.Spawns:WaitForChild("RedSpawns"):GetChildren()
	local BlueSpawns = ChoseMap.Spawns:WaitForChild("BlueSpawns"):GetChildren()
	print("It found the spawns")
	if player.Team.Name == "Really red" then
		if player and #RedSpawns > 0 then
			print("We got this far!")
			local UpperTorso = player.Character:WaitForChild("HumanoidRootPart")
			local AllSpawns = math.random(1, #RedSpawns)
			local RandomSpawn = RedSpawns[AllSpawns]
			if RandomSpawn and UpperTorso then
				table.remove(RedSpawns, AllSpawns)
				UpperTorso.CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
			end
		end
	elseif player.Team.Name == "Really blue" then
		if player and #BlueSpawns > 0 then
			print("We got this far!")
			local UpperTorso = player.Character:WaitForChild("HumanoidRootPart")
			local AllSpawns = math.random(1, #BlueSpawns)
			local RandomSpawn = BlueSpawns[AllSpawns]
			if RandomSpawn and UpperTorso then
				table.remove(BlueSpawns, AllSpawns)
				UpperTorso.CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
			end
		end
	end	
end

TeleportPlayer.OnServerEvent:connect(onTeleportPlayerFired)

They both print "It found the spawns" but then again they go no further.

I have another script that teleports players just fine and they are basically the exact same code.

	local RedSpawns = ChoseMap.Spawns.TeamSpawns.RedTeamLobbySpawns:WaitForChild("RedTeamSpawns"):GetChildren()
	for _, player in pairs(game.Players:GetChildren()) do
		if player.Team.Name == "Really Red" then
			if player and #RedSpawns > 0 then
				local UpperTorso = player.Character:WaitForChild("HumanoidRootPart")
				local AllSpawns = math.random(1, #RedSpawns)
				local RandomSpawn = RedSpawns[AllSpawns]
				if RandomSpawn and UpperTorso then
					table.remove(RedSpawns, AllSpawns)
					UpperTorso.CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
				end
			end
		end
	end

If anyone knows why please help. Its a simple fix I am sure.

Thank you for your time! :) And as always Merry Christmas!

I am almost 100% sure it errors here, but I dont know why.

if player.Team.Name == "Really red" then

2 answers

✓ Accepted answer

if you want to make them spawn on their spawns,

there's two ways.

:MoveTo() or :LoadCharacter()

  • :MoveTo()
if player.TeamColor = BrickColor.new("Really red") then
player.Character:MoveTo(RedSpawns.Position) -- edit this part
  • :LoadCharacter()
player:LoadCharacter()

5 comments

0

What if they are already spawned and they are in a lobby sorta thing?

0

It doesnt get past this line if player.Team.Name == "Really red" then

0

When the map gets inserted, try moving the spawns inside the lobby to somewhere (replicatedstorage), and then you can :LoadCharacter() and that'll make them spawn inside the map.

0

by the way, when you said that the third script works, i noticed "Really Red" instead of "Really red". Please note that ROBLOX is case sensitive. Try checking if that's the problem.

0

^ Yeah that was the problem. I forgot I capitalized both words in the team name. So it wasnt finding a team to teleport them too. Its working now, just a simple capitalization error. Ill accept your answer. Thanks dude!

I don't know what is wrong with your script, but instead of

if player.Team.Name == "Really red" then

try

if player.Team.TeamColor == "Really red" then --Or maybe it's .TeamColor.Name, IDK really

or something like that, I mean, instead of checking the name, check the color

1 comment

0

I mean, that's an alternative. By the way, it's "player.TeamColor = BrickColor.new("Really red")"

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