Script not teleporting player and im not getting errors

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.FinishEvent.OnServerEvent:Connect(function()
	local StageSelected = (math.random(1,3))
	if StageSelected == 1 then
		for i, children in pairs(game.ServerStorage.Map1:GetChildren()) do
			local newchild = children:Clone()
			newchild.Parent = game.Workspace.ActiveMap
		end
	end
	
	if StageSelected == 2 then
		for i, children in pairs(game.ServerStorage.Map2:GetChildren()) do
			local newchild = children:Clone()
			newchild.Parent = game.Workspace.ActiveMap
	end
	
	if StageSelected == 3 then
			for i, children in pairs(game.ServerStorage.Map3:GetChildren()) do
				local newchild = children:Clone()
				newchild.Parent = game.Workspace.ActiveMap
				end
			wait()
	for i, character in pairs(workspace.CharacterFolder:GetChildren()) do
					local randomizedNumber = math.random(1,4)
					character.Head.CFrame = CFrame.new(game.Workspace.ActiveMap["SpawnLocation"..randomizedNumber])
			end
		end
	end
end)

I’m using this script to teleport a player to the map after it’s put in the active map, and everything works up until the teleport. Nothing prints or anything, either. Any clue why this part isn’t running? I’m not getting any errors.

2 Likes

I am looking through the code rather carefully and I noticed several things:

  1. Under if StageSelected == 2 then, you forgot to put a end keyword at the end of the for loop.

  2. You forgot to put an end keyword for the if StageSelected == 3 then.

  3. You added two extra end keywords that you didn’t need for the final for loop which spawns the characters. This explains why you didn’t get any errors as you probably added the end keywords without giving too much thought as to what parts of the code is missing one.

While all of these will help you fix your code, the fact that you are basically duplicating code, with only changes to one or two things, is a very bad idea as, if you have to change or fix the underlying logic inside of the copied and pasted code, then you will have to do the same thing for every single instance, which can lead to problems maintaining the code and might eventually lead you to cancel your project, which is no fun.

Instead, I recommend doing the following things in the following order:

  1. For each map, create a Folder object and call it “StageData”, and place a IntValue object inside of it and name that “StageNumber”. This is incase you want to change the names of the maps later on.

  2. Place all of the maps inside of ServerStorage inside of another Folder object and name it “Maps”. You will be using that as a reference to get all of the maps. It also makes it where you don’t mix Maps with other stuff you need inside of ServerStorage if you need to later on.

  3. Create the following function and place in somewhere in the script you have given (before the ReplicatedStorage.FinishEvent.OnServerEvent:connect(... section of the code):

function PlaceStage(stageNumber) 

    -- This will get the map based on Stage Number.
    local selectedMap
    for i, map in pairs(game.ServerStorage.Maps:GetChildren()) do
        if (map.StageData.StageNumber.Value == stageNumber) then
            selectedMap = map
            break -- this is to make the program stop searching as it already found what it needed.
        end
    end

    -- This will then place the stage in Workspace.
    for i, children in pairs(map:GetChildren()) do
          local newchild = children:Clone()
          newchild.Parent = game.Workspace.ActiveMap
    end

end

  1. Finally, modify the script you have given to use this function, which, in the end, should look like this:
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--[[
    Places a map into Workspace given its stage number
--]]
function PlaceStage(stageNumber) 

    -- This will get the map based on Stage Number.
    local selectedMap
    for i, map in pairs(game.ServerStorage.Maps:GetChildren()) do
        if (map.StageData.StageNumber.Value == stageNumber) then
            selectedMap = map
            break -- this is to make the program stop searching as it already found what it needed.
        end
    end

    -- This will then place the stage in Workspace.
    for i, children in pairs(map:GetChildren()) do
          local newchild = children:Clone()
          newchild.Parent = game.Workspace.ActiveMap
    end

end

--[[
   Places a map onto Workspace and teleports players to it.
--]]
ReplicatedStorage.FinishEvent.OnServerEvent:Connect(function()

     local StageSelected = math.random(1,3)

     -- This is much better than repeating the same code over and over again,
     -- don't you think?
     placeStage(StageSelected)

     for i, character in pairs(workspace.CharacterFolder:GetChildren()) do
          local randomizedNumber = math.random(1,4)
          
          -- I changed this since this might be part of the problem.
          -- You can add a Vector3 to this variable to make corrections if needed.
          local newPosition = game.Workspace.ActiveMap["SpawnLocation"..randomizedNumber]
          character:SetPrimaryPartCFrame(CFrame.new(newPosition))
     end

end)

If this fixes your problem with teleportation, then great! However, if that is not the case, you can try changing the code to see what works - just don’t copy and paste the same code and make small changes to it as it will give you more of a headache later on when you try to fix a bug. Hope this helps!

Edit: I forgot to indent some code. when I looked at it I thought something was off but couldn’t put my finger on it, but I then realized I needed to indent some code. Whoops!

1 Like

I just realized I accidentally marked as solution.
I fixed a couple errors, but I’m getting one for line 43,

invalid argument #1 to 'new' (Vector3 expected, got Instance)

Nevermind, I fixed it __________

Would be nice if you shared how you fixed it (for others who visit the thread), I presume you just indexed the “Position” property of the referenced SpawnLocation instance.

Yes, that’s what I did. Here’s the finished script.

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

function PlaceStage(stageNumber) 

    local selectedMap
    for i, map in pairs(game.ServerStorage.Maps:GetChildren()) do
        if (map.StageData.StageNumber.Value == stageNumber) then
            selectedMap = map
            break
        end
    end

    for i, children in pairs(selectedMap:GetChildren()) do
          local newchild = children:Clone()
          newchild.Parent = game.Workspace.ActiveMap
    end

end

ReplicatedStorage.FinishEvent.OnServerEvent:Connect(function()

     local StageSelected = math.random(1,3)


     PlaceStage(StageSelected)

     for i, character in pairs(workspace.CharacterFolder:GetChildren()) do
          local randomizedNumber = math.random(1,4)
          

          local newPosition = game.Workspace.ActiveMap["SpawnLocation"..randomizedNumber].Position
          character:SetPrimaryPartCFrame(CFrame.new(newPosition))
     end

end)