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

Teleport HumanoidRootPart.CFrame with server script?

Asked by 4 years ago

It cant teleports. Output is "couldnt find player"

game.Players.PlayerAdded:Connect(function(plr)
    plyr = script.Parent

game.ReplicatedStorage.teleport.OnServerEvent:Connect(function()
        if plyr ~= nil then
            if plyr.WaitForChild("HumanoidRootPart") ~=nil then
            plyr.WaitForChild("HumanoidRootPart").CFrame = CFrame.new(16.5, 5.5, -11.5)
            print("humanoidrootpart is found")
        else
            print("couldnt find humanoidrootpart")
            return
            end
        else 
            print("couldnt find player")
        end
    end)
end)

1 answer

Log in to vote
0
Answered by 4 years ago

Why are you connecting a PlayerAdded event to a .OnServerEvent? .OnServerEvent passes the Player instance as the first parameter, so don't throw it away.

WaitForChild yields (makes the script wait) until it can find that instance it was waiting for. WaitForChild DOES NOT return a value, unlike FindFirstChild which returns nil if it can't find that instance, but WaitForChild is fine for this.


game:GetService("ReplicatedStorage").teleport.OnServerEvent:Connect(function(Player) Player.Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(16.5, 5.5, -11.5) print("humanoidrootpart is found") end)

WaitForChild is not indexed but is a function (sort of, i think) so you should use it with a colon.

0
Big thanks. Im newbie on LUA and this is help me for my skills Frenixer 0 — 4y
0
You're welcome, please mark as an answer if it helped. nekosiwifi 398 — 4y
Ad

Answer this question