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

Script prints but does not perform desired function?

function TeleportPlayerToMap()
    print("Teleport function works")
    for i, plr in pairs(AllPlayers) do
        print(plr.Name)
        if plr:FindFirstChild("Settings").AFK.Value ~= true then
            print(plr.Name.."is not AFK")
            if plr ~= nil then
                if plr.Character ~= nil then
                    if plr.Character:FindFirstChild("Humanoid").Health ~= 0 then
                        local spawns = CurrentMap:FindFirstChild(MapSelected.Value):FindFirstChild("Spawns"):GetChildren()
                        if spawns ~= nil then
                            local ransp = spawns[math.random(1,#spawns)].CFrame + Vector3.new(0,3,0)
                            plr.Character.HumanoidRootPart.CFrame = ransp
                            wait()
                        end
                    end
                end
            end
        end
    end
    print("Teleport complete")
end

So for some reason this script prints "Teleport function works" and "Teleport complete", however it does not teleport the player to the spawns on the map.

5 comments

1

Is this a Server Script? If it is, you can't call a player's character from the server the way you're attempting. Also the Teleport Complete is being printed since it's outside the function

0

This is a server script, and I didn't notice that it was outside the function mb

0

I'm working on a response now, can you tell me what the "CurrentMap" is? I can guess what allplayers is but idk if currentmap is a model or...?

0

"CurrentMap" is a string value inside of workspace that allows me to have the name of the map that was picked

0

Wait is this from a youtuber's minigame tutorial?

2 answers

✓ Accepted answer
function TeleportPlayerToMap()
	print("Teleport Function Begun")
	local AllPlayers = game.Players:GetPlayers()
	local CurrentMap = workspace.CurrentMap -- Change this to the location of "CurrentMap"
	for i, plr in pairs(AllPlayers) do
		print(plr.Name)
		local setting = plr:FindFirstChild("Settings")
		if setting ~= nil and setting:FindFirstChild("AFK") ~= nil and setting.AFK.Value ~= true then
		print(plr.Name.." is not AFK")
            if plr ~= nil and workspace:FindFirstChild(plr.Name) ~= nil and workspace[plr.Name]:FindFirstChild("Humanoid") ~= nil and workspace[plr.Name].Humanoid.Health ~= 0 then
				local selected = CurrentMap:FindFirstChild("MapSelected")
				local spawns  = nil
				if selected ~= nil and selected:FindFirstChild("Value") ~= nil and selected.Value:FindFirstChild("Spawns") ~= nil then
					spawns = selected.Value.Spawns:GetChildren()
				end
				if spawns ~= nil then
					local num = math.random(1, #spawns)
					local ransp = spawns[num].CFrame + Vector3.new(0, 3, 0)
					workspace[plr.Name].HumanoidRootPart.CFrame = ransp
					wait()
					print("Teleport Complete")
				end
			end
		end
	end
end

TeleportPlayerToMap() -- Change this to whatever you want to make the players teleport

So as I said earlier in the comments, the "Teleport Complete" print was only firing once the function was created.

You had a lot of FindFirstChild checks that automatically assumed it was not nil, so I adjusted the if-then clauses to account for these.

The "AllPlayers" calling of players should be inside the function since the players are obviously at some point going to change since the server was created. This is most likely the reason the player's name was not printed.

Since CurrentMap is a StringValue that will likely change, I placed this inside the function as well.

2 comments

0

This function works up to line 10 "if plr ~= nil and workspace:FindFirstChild(plr.Name) ~= nil and workspace[plr.Name]:FindFirstChild("Humanoid") ~= nil and workspace[plr.Name].Humanoid.Health ~= 0 then". At this point it doesnt print anything (I added a print) and the player doesnt get teleported

0

Edit: I fixed it there was a problem with the selected variable

I can see that you haven't called your function for this we need to do:

TeleportPlayerToMap() Below all of the ends.

2 comments

0

wrong, line 12 is the problem

0

What is the issue with line 12?

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