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

Why isn't my GUI Button teleport the player random?

Asked by 5 years ago

Recently I've been messing around with GUI Buttons in scripting and decided to do a random teleport. I had another place where I was working on but decided to open a new studio instance and play around with it. The teleport worked but now it isn't. How do I fix it?

It says Torso is not a part of the Model and I don't now what it exactly means.

local screenGui = Instance.new("ScreenGui")
screenGui.Parent = script.Parent

local textButton = Instance.new("TextButton")
textButton.Parent = screenGui
textButton.Position = UDim2.new(0, 25, 0, 300)
textButton.Size = UDim2.new(0, 150, 0, 50)
textButton.BackgroundColor3 = BrickColor.White().Color
textButton.Text = "Random Teleport"

    textButton.MouseButton1Down:Connect(function()
    game:GetService('UserInputService')
    game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(Vector3.new(Random, Random, Random))
    textButton.Visible = false

end)
0
i dont think connect is capitalized ncano 22 — 5y
0
it worked with it being capitalized on the other place BananaP0tato 11 — 5y
0
The error pretty much tells you everything. “Torso” doesn’t exist in the player’s character. Your script should work with R6 characters only, but if you’re using R15 too, use HumanoidRootPart instead. User#20279 0 — 5y
0
also, this isnt FE compatable theking48989987 2147 — 5y
0
Are you using R15 rig or R6? The errors tells yoy it cant find anything named Torso. The model is the player model in Workspace when you play. If your rig is R15 there are "UpperTorso"&"LowerTorso". R6 has just Torso. HeyItzDanniee 252 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

While testing this script, it seems the (Random, Random, Random) always just put me in the same position I already am.

Since you want to teleport a player, presumably within a map, you can use the GetExtents() Property of models to help generate a random placement with math.random()

Local Script

local screenGui = Instance.new("ScreenGui")
screenGui.Parent = script.Parent

local textButton = Instance.new("TextButton")
textButton.Parent = screenGui
textButton.Position = UDim2.new(0, 25, 0, 300)
textButton.Size = UDim2.new(0, 150, 0, 50)
textButton.BackgroundColor3 = BrickColor.White().Color
textButton.Text = "Random Teleport"

textButton.MouseButton1Down:Connect(function()
    local extent = workspace.Model:GetExtentsSize()
    local random1 = math.random(-(extent.X)/2, (extent.X)/2)
    local random2 = math.random(-(extent.Y)/2, (extent.Y)/2)
    local random3 = math.random(-(extent.Z)/2, (extent.Z)/2)
    game.Players.LocalPlayer.Character:SetPrimaryPartCFrame(CFrame.new(random1, random2, random3))
    textButton.Visible = false
end)

Using the SetPrimaryPartCFrame() is better so that it will work for all players, regardless of their rigtype (R6 / R15)

Ad

Answer this question