There’s something wrong with this script and I don’t know what is It can you help me to know what is wrong with this script and thank you who answered.
– This script is intended to move a part up and down
local speed = 5
local height = 10
local direction = 1
while true do
wait(0.5)
part.Position = part.Position + Vector3.new(0, direction * speed, 0)
if part.Position.Y >= height then
direction = -1
elseif part.Position.Y <= 0 then
direction = 1
end
end
What's wrong??
Please use the correct category for your posts, which would be scripting support.
Use ``` when you are posting a script, otherwise it is pretty much unreadable.
You are trying to change the variable of an active loop. You cannot do this, so your first step would be to bring your variables out of your loop, and rather than doing while true do wait(0.5) you can just do while task.wait(0.5) do
To be honest you should just use tweenservice though. Much more reliable, and way easier.
Here is how and example of how to use TweenService:
local part = script.Parent -- put the part here
local MaxHeight = 10
local Duration = 2 -- time to complete the movement
local easingStyle = Enum.EasingStyle.Sine
local easingDirection = Enum.EasingDirection.InOut
local function MovePart() -- the main function
local tweenInfoUp = TweenInfo.new(Duration, easingStyle, easingDirection)
local tweenInfoDown = TweenInfo.new(Duration, easingStyle, Enum.EasingDirection.In)
local tweenUp = game:GetService("TweenService"):Create(part, tweenInfoUp, {Position = part.Position + Vector3.new(0, MaxHeight, 0)})
tweenUp:Play()
tweenUp.Completed:Wait()
local tweenDown = game:GetService("TweenService"):Create(part, tweenInfoDown, {Position = part.Position - Vector3.new(0, MaxHeight, 0)})
tweenDown:Play()
tweenDown.Completed:Wait()
end
while true do
MovePart()
end