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

Why isnt my part moving up? [SOLVED]

function Elevator()
	for i= 1, 100, 2 do
	game.Workspace.kelevator.Position = Vector3.new(1.85, i, 75.6)
	end
end
	
	
	script.Parent.ClickDetector.MouseClick:Connect(function(Elevator)
		
	end)

I want my "kelevator" part to move up when i click on the click detector but nothing happens when i click on it

2 comments

0

You don't have to put a function() since Elevator is already one: script.Parent.ClickDetector.MouseClick:Connect(Elevator)

0

also if you want smooth ascending use CFrame or tweening

1 answer

✓ Accepted answer

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

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