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

Why doesn't the part go to the position I want it to go?

Got this off roblox-wiki yet funnily enough, the part goes in a completely opposite direction to where I want it to go. Please can someone help me to fix? Does not show any errors or anything like that! The script is used after another identicular script but with a different position.

The code: (The code is in a script in a part, I also added some stuff to it for when it is completed it's movements.)



local Part =  script.Parent


local newPos = Vector3.new(30.976, 6.022, 8.345)
local Time = 1
local Increment = 0.5 
local Debounce = false
 
local Diff = newPos - Part.Position 
local Mag = Diff.magnitude 
local Direction = CFrame.new(Part.Position, newPos).lookVector
 
function MovePart() 
	if Debounce then return end 
	Debounce = true 
	for n = 0, Mag, Increment do
		Part.CFrame = Part.CFrame + (Direction * Increment)
		wait( (Time/Mag) * Increment )
	end
	script.Parent.Orientation = Vector3.new(0, -22.18, 0)
 	Part.Position = Vector3.new(30.976, 6.022, 8.345)
	
	Debounce = false 
end

game.Workspace.AnimationValues.Jump.Changed:Connect(function() 
	MovePart()
end)

2 comments

0

you're using Vector3s for a CFrame at line 11

0

^That's totaly fine, but you should use (Part.Position - newPos).Unit instead.

2 answers

✓ Accepted answer

If you want to cheat and make things TOO easy, here's a script:

part = script.Parent

local goal = {}
goal.Position = Vector3.new(30.976, 6.022, 8.345)

local Time = 1
local TweenStyle = Enum.EasingStyle.Linear
local TweenDirection = Enum.EasingDirection.InOut
local tweenInfo = TweenInfo.new(Time, TweenStyle, TweenDirection)

local debounce = false

workspace.AnimationValues.Jump.Changed:Connect(function()
	if debounce then return end
	debounce = true
	local Animation = game:GetService("TweenService"):Create(part, tweenInfo, goal)
	Animation:Play()
	wait(Time)
	debounce = false
end)

Hope this helped!

Replace anything that says position with CFrame... like this:

local Part =  script.Parent


local newPos = CFrame.new(30.976, 6.022, 8.345)
local Time = 1
local Increment = 0.5 
local Debounce = false

local Diff = newPos - Part.CFrame 
local Mag = Diff.magnitude 
local Direction = CFrame.new(Part.Position, newPos).lookVector

function MovePart() 
    if Debounce then return end 
    Debounce = true 
    for n = 0, Mag, Increment do
        Part.CFrame = Part.CFrame + (Direction * Increment)
        wait( (Time/Mag) * Increment )
    end
    script.Parent.Orientation = Vector3.new(0, -22.18, 0)
    Part.CFrame = Vector3.new(30.976, 6.022, 8.345)

    Debounce = false 
end

game.Workspace.AnimationValues.Jump.Changed:Connect(function() 
    MovePart()
end)

1 comment

0

Doesn't work, gives out this error: bad argument #2 to '?' (Vector3 expected, got CFrame)

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