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

How to teleport a player to a random area of a base upon click?

Asked by
stix619 15
10 years ago

To expand upon my other answered question, I want to teleport someone who clicks a text button to a random area on the baseplate that I specify (i.e. a spawn point ). I want to try a table and pull random values from that, but I'm not sure that it will work.

The current code I have and want to edit is below.

bin = script.Parent
Part = game.Players.LocalPlayer.Character

function onClick()
    if Part.FindFirstChild("Torso") then
        Part.Torso.CFrame = CFrame.new(Vector3.new(6, -28, 2)
    end
end

bin.MouseButton1Click:connect(onClick)

If you could, please comment the changes you made to the script, and highlight the things I did wrong. Thanks in advance.

2 answers

Log in to vote
1
Answered by 10 years ago

I fixed your mistakes, you put .FindFirstChild when FindFirstChild is a method, therefore it needs ':', so :FindFirstChild, also you were missing a ')' after the line with Part.Torso.CFrame.

Also change the base variable to the place you want them to spawn to!

bin = script.Parent
Part = game.Players.LocalPlayer.Character
Base = Workspace.BasePlate

function onClick()
    if Part:FindFirstChild("Torso") then
        local MinX = Base.Position.X-(Base.Size.X/2)
        local MaxX = Base.Position.X+(Base.Size.X/2)
        local MinZ = Base.Position.Z-(Base.Size.Z/2)
        local MaxZ = Base.Position.Z+(Base.Size.Z/2)
        Part.Torso.CFrame = CFrame.new(Vector3.new(math.random(MinX,MaxX), Base.Position.Y+3, math.random(MinZ,MaxZ)))
    end
end

bin.MouseButton1Click:connect(onClick)

0
The game doesn't seem to like line 11, with the output saying "Players.Player1.PlayerGui.ScreenGui.TextButton.Script:11: bad argument #2 to 'random' (number expected, got nil)". stix619 15 — 10y
0
Did you change the Base variable to the correct part..? nikilis 5 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
bin = script.Parent
Part = game.Players.LocalPlayer.Character

function onClick()
    if Part:FindFirstChild("Torso") then
        Part.Torso.CFrame = CFrame.new(Vector3.new(6, -28, 2)
    end
end

bin.MouseButton1Click:connect(onClick)

Answer this question