Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script prints but does not perform desired function?

Asked by 5 years ago
Edited 5 years ago
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.

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 SerpentineKing 3885 — 5y
0
This is a server script, and I didn't notice that it was outside the function mb iiBluShadows 2 — 5y
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...? SerpentineKing 3885 — 5y
0
"CurrentMap" is a string value inside of workspace that allows me to have the name of the map that was picked iiBluShadows 2 — 5y
0
Wait is this from a youtuber's minigame tutorial? MahadTheIronSword 98 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
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.

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 iiBluShadows 2 — 5y
0
Edit: I fixed it there was a problem with the selected variable iiBluShadows 2 — 5y
Ad
Log in to vote
0
Answered by
Cyrakohl 108
5 years ago

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

TeleportPlayerToMap() Below all of the ends.

0
wrong, line 12 is the problem DeceptiveCaster 3761 — 5y
0
What is the issue with line 12? iiBluShadows 2 — 5y

Answer this question