Search code examples
luarobloxteleport

Roblox Studio function does not recognize character humanoid part even though debugging shows it


I am trying to teleport all characters to specific points and have tried putting the script in ServerScriptService, as well as WorkSpace, while testing. I have also tried writing my code a few different ways and keep getting the same fault codes "Humanoid is not valid part of (my character's name)" and "attempted to index nil". I'm going to continue writing it a few ways and testing, but out of all the forums I've looked through and different code formats I've tried nothing seems to work. Below is my current script for reference and any help is greatly appreciated.

spawnMH = Vector3.new(0.578, 42.203, 24.559)
spawnBattle = {Vector3.new(-403.386, -1.739, 1098.992), Vector3.new(855.657, 6.303, 1177.902), Vector3.new(1114.807, 3.436, -243.573), Vector3.new(956.57, 19.462, 912.349), Vector3.new(-990.968, 3.027, 356.449), Vector3.new(-1152.185, 52.029, 781.85)}

function InitTeleportDay()  
    for _, player in pairs(game.Players:GetPlayers()) do
        workspace:WaitForChild(player.Name)
        local character = player.Character or player.CharacterAdded:Wait()
        local humanoid = character:WaitForChild("Humanoid")
        humanoid:MoveTo(spawnMH)
    end
end

function InitTeleportNight()
    for _, player in pairs(game.Players:GetPlayers()) do
        workspace:WaitForChild(player.Name)
        local character = player.Character or player.CharacterAdded:Wait() 
        local humanoid = character:WaitForChild("Humanoid")
        humanoid:MoveTo(spawnBattle.get(Random.new(5))) 
    end
end

while true do
    wait(1*60)
    InitTeleportDay()
    wait(1*60)
    InitTeleportNight()
end
ServerScriptService.TeleportFunc:22: attempt to call a nil value - server - TeleportFunc:22
Humanoid is not a valid member of Model "maxeagleman" - Client

Above are the what the debugger shows me for faults. I feel like this should be super simple and the code is not that long, but the way it's processing the code it doesn't like. I've tried just using character:MoveTo(the CFrame value here), character:MoveTo(the Vector3 value here), character.Humanoid:MoveTo(the CFrame value here), character.Humanoid:MoveTo(character, the CFrame value here), character.position = CFrame value here, ..... all to no avail.

I'm going to keep tweaking and testing and if I come up with the solution I'll include an update.


Solution

  • You are currently trying to call the function get that doesn't exist on the spawnbattle table. You are also attempting to get a random number by using Random.new(5) creates a class not a random integer, to do that you will want to use math.random instead.

    This means that instead of this line:

    humanoid:MoveTo(spawnBattle.get(Random.new(5))) 
    

    You should use

    humanoid:MoveTo(spawnBattle[math.random(1,#spawnBattle)]) 
    

    You can find information about random numbers here: https://create.roblox.com/docs/reference/engine/datatypes/Random

    However, this causes the player's character to walk to the specified location, while I believe you want them to teleport there. This means instead of humanoid:MoveTo you should use character:MoveTo()

    This means your final script should be:

    spawnMH = Vector3.new(0.578, 42.203, 24.559)
    spawnBattle = {Vector3.new(-403.386, -1.739, 1098.992), Vector3.new(855.657, 6.303, 1177.902), Vector3.new(1114.807, 3.436, -243.573), Vector3.new(956.57, 19.462, 912.349), Vector3.new(-990.968, 3.027, 356.449), Vector3.new(-1152.185, 52.029, 781.85)}
    
    function InitTeleportDay()  
        for _, player in pairs(game.Players:GetPlayers()) do
            workspace:WaitForChild(player.Name)
            local character = player.Character or player.CharacterAdded:Wait()
            local humanoid = character:WaitForChild("Humanoid")
            character:MoveTo(spawnMH)
        end
    end
    
    function InitTeleportNight()
        for _, player in pairs(game.Players:GetPlayers()) do
            workspace:WaitForChild(player.Name)
            local character = player.Character or player.CharacterAdded:Wait() 
            print(character)
            local humanoid = character:WaitForChild("Humanoid")
            character:MoveTo(spawnBattle[math.random(1,#spawnBattle)]) 
        end
    end
    
    while true do
        wait(1*60)
        InitTeleportDay()
        wait(1*60)
        InitTeleportNight()
    end