I am trying to make a simple elevator using CFrame, as I am just starting out with LUA. I am trying to make it so when you chat a message the script will execute, but it is just not working. I know it is a long script, but I haven't figured out how to shorten it.
p.Chatted:connect(function(msg) if msg=="://Down" then game.Workspace.Part1.CFrame = CFrame.new(0, 56, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 55, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 50, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 45, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 40, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 35, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 30, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 25, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 20, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 15, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 10, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 5, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 0, 0.5) wait (.1) elseif msg=="://Up" then game.Workspace.Part1.CFrame = CFrame.new(0, 0, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 5, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 10, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 15, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 20, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 25, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 30, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 35, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 40, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 45, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 50, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 55, 0.5) wait (.1) game.Workspace.Part1.CFrame = CFrame.new(0, 56, 0.5) wait (.1) (end end)
Well, first let's shorten this. You can do this using for
loops. These are loops that will only run a specific amount of times. You can see them in action below.
p.Chatted:connect(function(msg) if msg == "://Down" then for i = 1, 11 do -- Adjust the '11' to however many times you want the for loop to run. game.Workspace.Part1.CFrame = game.Workspace.Part1.CFrame * CFrame.new(0, -5, 0) wait(0.1) end elseif msg=="://Up" then for i = 1, 11 do -- Adjust the '11' to however many times you want the for loop to run. game.Workspace.Part1.CFrame = game.Workspace.Part1.CFrame * CFrame.new(0, 5, 0) wait(0.1) end end end)
Note the line game.Workspace.Part1.CFrame = game.Workspace.Part1.CFrame * CFrame.new(0, -5, 0)
. That code will take the existing CFrame and add to it. Here, we're adding -5, so it goes down.
Also, have you defined p
? If not, an easy way to do that would be to add this to the start:
local p = game.Players.LocalPlayer
Note that this requires your code to be in a LocalScript. You should put the local script somewhere like StarterPlayerScripts, which is inside StarterPlayer.