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

Whats wrong with my teleport script? [UNSOLVED]

Asked by
Bman8765 270 Moderation Voter
10 years ago

Okay so I have been working on this code all day but for some reason the teleport script wont work, everything else works fine but when a click on the button and trigger the function it does not teleport me it just closes all the gui's (Which is should do but still it should also teleport).

the main script:

local character = game.Players.LocalPlayer.Character
Start = script.Parent.StartButton
CourseSelect = script.Parent.CourseSelect
target = game.Workspace.Cam
camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = target
angle = 0
go = 0
CourseSelect.Visible = false
Num = 0
Course1 = script.Parent.CourseSelect.Course1
Course2 = script.Parent.CourseSelect.Course2
Course3 = script.Parent.CourseSelect.Course3
Course1.Visible = false
Course2.Visible = false
Course3.Visible = false
-- Size X 400, Size Y 300
print("Started = false")

function ClickedStart()
    CourseSelect.Visible = true
    Start.Visible = false
    for GuiSize = 1, 45 do 
        if GuiSize <= 45 then
            CourseSelect.Size = CourseSelect.Size +UDim2.new(0.01, 0, 0.015, 0)
            Num = Num +1
            print(Num)
        end
     wait()
    end
    Course1.Visible = true
    Course2.Visible = true
    Course3.Visible = true
end

function ClickedCourse1()
    Course1.Visible = false
    Course2.Visible = false
    Course3.Visible = false
    CourseSelect.Visible = false
    wait(3)
    character.CFrame = CFrame.new(game.Workspace.Course1Tele.Position + Vector3.new(0,3,0))
end

Course1.MouseButton1Down:connect(ClickedCourse1)
Start.MouseButton1Down:connect(ClickedStart)

Like I have said above, everything else works so if you have any suggestions please help. I should also add that this works fine in Solo mode but when I publish it to roblox that is when it wont teleport. Also I don't see anything bad appear in the output console so I'm clueless on whats going on.

0
This is the error I get: attempt to index global 'character' (a nil value) Bman8765 270 — 10y

2 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

If the character is nil, it's most likely because the script runs before the character is loaded. To fix this, either wait a few seconds before defining the variable, or do this:

repeat wait() until game.Players.LocalPlayer.Character --Waits until the character is not nil.
local character = game.Players.LocalPlayer.Character

EDIT:

On line 43, you try to change the CFrame of the character. However, the character is a model. A model does not have a CFrame, only a brick does.

Solution: Use the MoveTo() method on the character model, or change the CFrame of the character's torso.

So,

character:MoveTo(game.Workspace.Course1Tele.Position + Vector3.new(0,3,0))

Or,

character.Torso.CFrame = CFrame.new(game.Workspace.Course1Tele.Position + Vector3.new(0,3,0))
0
Now I'm getting this error attempt to index upvalue 'character' (a nil value) Bman8765 270 — 10y
0
What line? Perci1 4988 — 10y
0
That line I put your code on, I also moved both scripts and put them together for reference. Line: 43 Bman8765 270 — 10y
0
Changed script above so you understand what it looks like now Bman8765 270 — 10y
0
Read it now Perci1 4988 — 10y
Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

I've never really liked the MoveTo method, it's always given me problems and isn't as easy to manipulate as say just CFraming the model. Try this..

character.Torso.CFrame = CFrame.new(game.Workspace.Course1Tele.Position + Vector3.new(0,3,0))
--Add 3 to the y scale so they don't get stuck inside the part

Not that I even saw anything really wrong with either of those scripts, just this is another way that I know works.

I know this isn't a lot to help seeing as how I don't know what was wrong but just try this and hope it works, if not tell me.

-Goulstem

0
Not working, and I don't know why. I keep trying everything but it just wont work when I publish it? Should this be in a regular script instead of local? Bman8765 270 — 10y
0
Here is the error I getPlayers.Player1.PlayerGui.StartingScreen.LocalScript:42: attempt to index global 'character' (a nil value) Bman8765 270 — 10y

Answer this question