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

Teleport to Part.Position onClick?

Asked by
iNicklas 215 Moderation Voter
9 years ago

ERROR: 16:43:23.107 - Players.Player.PlayerGui.Hole.Frame.GreenButton.Green.Scrip:6: attempt to index local 'hit' (a nil value)

local Btn = script.Parent
local Gui = Btn.Parent.Parent.Parent
TeleportPart = workspace["4"]

function Click(hit)
    print(hit.Parent)
local humanfound = hit.Parent:FindFirstChild("Humanoid")
    local playerfound = game.Players:GetPlayerFromCharacter(hit.Parent)
    if hit.Parent and humanfound and humanfound.Health > 0 and
playerfound then
        hit.Parent:MoveTo(TeleportPart.Position)
    end
end


Btn.MouseButton1Click:connect(Click)
0
I'm not so sure if this will help, but on Roblox wiki obby lesson, it shows how to make a teleporter. Check it out. GreekGodOfMLG 244 — 9y

1 answer

Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
9 years ago

Your problem is that there are no arguments in this specific event/function. Therefore, we simply need to get the player a different way. Make sure that this is in a LocalScript, and follow what I do.


Getting the player

First, we need to simply get the player. To do this, we'll use LocalPlayer. This gets the player from a LocalScript, as long as it's a descendent of the Player, the Backpack, or the PlayerGui. Here's what it will look like:

Player=game.Players.LocalPlayer

So now we have that set. Onto the next part.


Setting Up The Function

For this part, we simply need to setup the function differently. Here's how it should look:

function Click()
    if Character:FindFirstChild("Humanoid") then
        local Humanoid=Character.Humanoid
        if Humanoid.Health > 0 then
            Character.HumanoidRootPart.CFrame=CFrame.new(Vector3.new(TeleportPart.Position))
        end 
    end
end


Btn.MouseButton1Click:connect(Click)

So basically, that's it.


Final Product

Now, we'll put the entire script together for you:

local Btn = script.Parent
local Gui = Btn.Parent.Parent.Parent
TeleportPart = workspace["4"]

Player=game.Players.LocalPlayer
repeat wait() until Player.Character
Character=Player.Character

function Click()
    if Character:FindFirstChild("Humanoid") then
        local Humanoid=Character.Humanoid
        if Humanoid.Health > 0 then
            Character.HumanoidRootPart.CFrame=CFrame.new(Vector3.new(TeleportPart.Position))
        end 
    end
end

Btn.MouseButton1Click:connect(Click)

Anyways, hope I helped, if you have any further questions/problems, please leave a comment below :P

-Dyler3

Ad

Answer this question