Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is wrong with this level based teleport script?

this is the first script i have ever created. i thought i did pretty well. it doesn't work though, does anyone know why?

local player = game:GetService("Players").LocalPlayer -- Getting the local player
local char = player.Character or player.CharacterAdded:Wait() -- Waiting for character

local level = 0    -- The value for the player's level
local required = 1 -- The required level for the player (can be changed)
local guiButton = game.StarterGui.ScreenGui.TextButton -- The directory to the Gui TextButton
local goal = 43.55, 1.9, 106.9 -- The desired position, either A Vector3 or a CFrame

guiButton.MouseButton1Down:connect(function() -- If the gui button is pressed (left click)
    if level < required then
        return end
    end) 
if level >= required then
        local hum = char:FindFirstChild("HumanoidRootPart")
        hum.CFrame = goal (43.55, 1.9, 106.9) -- If CFrame
end

1 comment

0

for starts, you are using StarterGui instead of the localplayer's playergui

2 answers

Your ends are all over the place and wasn't opening and closing your spaces correctly. Indention can help a lot to avoid this.

Also you aren't setting their cframe correctly.

As well as setting the guiButton incorrectly, this is because the contents of StarterGui are cloned to the PlayerGui which can be accessed with player.

local player = game:GetService("Players").LocalPlayer -- Getting the local player
local char = player.Character or player.CharacterAdded:Wait() -- Waiting for character

local level = 0    -- The value for the player's level
local required = 1 -- The required level for the player (can be changed)
local guiButton = player.PlayerGui.ScreenGui.TextButton -- The directory to the Gui TextButton
local goal = Vector3.new(43.55, 1.9, 106.9) -- The desired position, either A Vector3 or a CFrame

guiButton.MouseButton1Down:connect(function() -- If the gui button is pressed (left click)
    if level < required then
        return end
    end
    if level >= required then
        local hum = char:FindFirstChild("HumanoidRootPart")
        hum.CFrame = CFrame.new(goal) -- If CFrame
    end
end)
local guiButton = game.StarterGui.ScreenGui.TextButton -- The directory to the Gui TextButton

This will not work. You have to change the TextButton in the PlayerGui, so it would probably be easiest to place this script inside of game.StarterGui.ScreenGui.TextButton and then change guiButton to access

local guiButton = script.Parent

You can't assign events to things in StarterGui because it won't carry over when duplicated to PlayerGui.

Here's the completed code. Also remember to store things in Vector3's, and to call CFrames correctly.

local player = game:GetService("Players").LocalPlayer -- Getting the local player
local char = player.Character or player.CharacterAdded:Wait() -- Waiting for character

local level = 0    -- The value for the player's level
local required = 1 -- The required level for the player (can be changed)
local guiButton = script.Parent -- Put this script into the button.
local goal = Vector3.new(43.55, 1.9, 106.9) -- The desired position, either A Vector3 or a CFrame

guiButton.MouseButton1Down:connect(function() -- If the gui button is pressed (left click)
    if level < required then
        return end
    end) 
if level >= required then
        local hum = char:FindFirstChild("HumanoidRootPart")
        hum.CFrame = CFrame.new(goal) -- If CFrame
end

1 comment

0

i can't get it to work, i did what you said and put a screengui in startergui, put a textbutton in that screengui, and i inserted the script you said to put in the textbutton. i copied your edited script and put it in workspace. what have i done wrong?

This discussion is preserved as it was. New answers, comments, and votes are closed.