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

Can someone please explain with comments next to each line what they do?

I made this teleport script a while ago when I was learning lua. I stopped scripting for some time and have gone rusty. I cannot understand what some of these lines do. Can someone please explain to me with comments next to each line? Thanks.

button = script.Parent
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Torso
torso = player.Character.Torso

function toReturn()
	torso.CFrame = game.workspace.Teleport2.CFrame
end

button.MouseButton1Click:connect(toReturn)

1 answer

✓ Accepted answer

###Yes I can.

local button = script.Parent -- use more local variables. here we are getting the button
local player = game:GetService("Players").LocalPlayer -- getting the player. this is localscript only

local char = player.Character or player.CharacterAdded:Wait() -- get character. wait for it, or if already loaded, assign char plr.Character.
local hrp = char.HumanoidRootPart -- R15 rigs don't have a torso. but all rigs have HumanoidRootPart

local function toReturn() -- create a function

    hrp.CFrame = game.Workspace.Teleport2.CFrame -- teleports character to specified CFrame

end -- end of function

button.MouseButton1Click:Connect(toReturn)
-- connects function to event. also, :connect is deprecated, switch to :Connect

1 comment

0

Thanks for the help, I also appreciate the tweaks in my code. It looks nicer.

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