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)
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)