Revised Script
function Elevator()
for i = 0, 100, 2 do
workspace.kelevator.Position = Vector3.new(1.85, i, 75.6)
end
end
script.Parent.ClickDetector.MouseClick:Connect(Elevator)
You've pre-defined "Elevator" as a function, so you don't need to call it again. Also, 100 - 1 is odd (99), but you have an even numbered increment
Since you have a for-loop, you may want to add a wait time inside it so that the "kelevator" doesn't just immediately move to the final location.
DEBOUNCE
You can add a "debounce" to make sure that the function doesn't get overridden if the player spams the clickdetector.
local debounce = true
function Elevator()
if debounce == false then return end
debounce = false
for i = 0, 100, 2 do
workspace.kelevator.Position = Vector3.new(1.85, i, 75.6)
wait()
end
debounce = true
end
script.Parent.ClickDetector.MouseClick:Connect(Elevator)
FUNCTIONS
You can also define the function as you use it, rather than take up an extra line to fire the event.
local debounce = true
script.Parent.ClickDetector.MouseClick:Connect(function(player)
if debounce == false then return end
debounce = false
for i = 0, 100, 2 do
workspace.kelevator.Position = Vector3.new(1.85, i, 75.6)
wait()
end
debounce = true
end)
OTHER OPTIONS
Tweens can be used to incrementally move a part smoothly from one place to another
Tween (Position Vector)
local TweenService = game:GetService("TweenService")
local timer = (2/100)
local tweenInfo = TweenInfo.new(timer)
local debounce = true
script.Parent.ClickDetector.MouseClick:Connect(function(player)
if debounce == false then return end
debounce = false
local elevator = workspace.kelevator
local goal = {}
goal.Position = Vector3.new(1.85, 100, 75.6)
local tween = TweenService:Create(elevator, tweenInfo, goal)
tween:Play()
wait(timer)
debounce = true
end)
However, Position is prone to being moved when the part is colliding with another, so its probably better to use a CFrame
Tween (CFrame)
local TweenService = game:GetService("TweenService")
local timer = (2/100)
local tweenInfo = TweenInfo.new(timer)
local debounce = true
script.Parent.ClickDetector.MouseClick:Connect(function(player)
if debounce == false then return end
debounce = false
local elevator = workspace.kelevator
local goal = {}
goal.CFrame = CFrame.new(1.85, 100, 75.6)
local tween = TweenService:Create(elevator, tweenInfo, goal)
tween:Play()
wait(timer)
debounce = true
end)
All above examples assume "kelevator" is a Part and NOT a Model
2 comments
You don't have to put a function() since Elevator is already one: script.Parent.ClickDetector.MouseClick:Connect(Elevator)
also if you want smooth ascending use CFrame or tweening