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

Teleporting not working properly or something?

Asked by 9 years ago

I just tried to make a brick that teleports you back to a position of (5, 5, 5), I tested in Play Solo mode and it works for around five time but then after that it doesn't work anymore. It gave me this error:

17:09:46.732 - Workspace.Part.Script:11: attempt to index global 'player' (a nil value) 17:09:46.733 - Stack Begin 17:09:46.734 - Script 'Workspace.Part.Script', Line 11 17:09:46.737 - Stack End

And here is my script

local isOn = false

target = CFrame.new(5, 5, 5)


function teleport(hit)
    if not isOn then
        isOn = true
    name = hit.Parent.Name
    player = game.Workspace:FindFirstChild(name)
    player.Torso.CFrame = target
    isOn = false
 end
end

script.Parent.Touched:connect(teleport)

1 answer

Log in to vote
1
Answered by
Yvori 25
9 years ago

It's possible that something entirely unrelated to a character is touching the brick, especially if it's unanchored. If said object is in Workspace directly, and not part of a model, the script can't find it's parent in Workspace because it IS Workspace. It's like looking for a box inside itself.

I suggest adding an if statement to make sure that what it's finding actually exists, and that it is in fact a character. Here's an example:

local isOn = false

target = CFrame.new(5, 5, 5)

function teleport(hit)
    if not isOn then
        isOn = true
        local name = hit.Parent.Name
        if Workspace:findFirstChild(name) then
            local player = Workspace:findFirstChild(name)
            if player:findFirstChild("Humanoid") and player:findFirstChild("Torso") then
                player.Torso.CFrame = target
            end
        end
        isOn = false
    end
end

script.Parent.Touched:connect(teleport)
0
Ok, thanks. It works now XDarkstarDX 10 — 9y
Ad

Answer this question