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

Invalid player to teleport?

Asked by 2 years ago
Edited 2 years ago

Hello im a new scripter im still learning and im try to make a touch part teleport script for my game but when i run it and check the output it says invalid player i dont know how fix it

here code

local TeleportService = game:GetService("TeleportService")
local place = Removed place id

function onTouch(hit)
    local Players = game:GetAttributes(hit)

    TeleportService:Teleport(place, hit , Players)

end
script.Parent.Touched:connect(onTouch)

i really dont know how fix it i send help to my friend but he dont know too.

1 answer

Log in to vote
0
Answered by 2 years ago

Hi superplayer1453! I see what's wrong here!

You seems to take a player from a attribute, but the attribute doesn't exist, you cannot create a attribute holding a object-typed variable.

here my solution with comments

local TeleportService = game:GetService("TeleportService")
--we will need this service to get the player
local players = game:GetService("Players")

local place = Removed place id

function onTouch(hit)
    --the hit variable will return for exemple left leg, right leg or smth else,
    --so we check if for exemple left leg has a parent and if the parent has a humanoid
    --if it has a humanoid it means a player touched the part.
    if hit.Parent and hit.Parent:WaitForChild("Humanoid") then
        --this means the character model is the parent of left leg.
        local char = hit.Parent
        --Then we use a built-in function to get the player object by giving the char.
        local plr = players:GetPlayerFromCharacter(char)
        TeleportService:Teleport(place, plr)
    end
end
script.Parent.Touched:connect(onTouch)
0
Oh wow thanks! superplayer1453 -4 — 2y
0
No problem ! TheSuperDiamond77 30 — 2y
Ad

Answer this question