Tween going to (0,0,0) every time

I’m trying to make it so that when a player is within a certain distance from an orb it will automatically go to the player.

For some reason when the player gets within the distance the tween goes to the origin of the world (0,0,0)

Why?

Code:

local ThisPart = script.Parent
local can = true

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math.random(1, #players)]

while wait() do
	local PlrPosition = pickedPlayer.Character.PrimaryPart.Position

	Distance = (ThisPart.Position - PlrPosition).Magnitude
	if Distance < 10 and can == true then

		can = false

		local TweenService = game:GetService("TweenService")

		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0,false,0)

		local tween = TweenService:Create(ThisPart, tweenInfo, {Position = Vector3.new(PlrPosition)})

		tween:Play()
	end
end

HELP!!

Try using :FindFirstChild(“HumanoidRootPart”) and not PrimaryPart.

local ThisPart = script.Parent
local can = true

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math.random(1, #players)]

while wait() do
	local PlrPosition = pickedPlayer.Character:FindFirstChild("HumanoidRootPart").Position

	Distance = (ThisPart.Position - PlrPosition).Magnitude
	if Distance < 10 and can == true then

		can = false

		local TweenService = game:GetService("TweenService")

		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0,false,0)

		local tween = TweenService:Create(ThisPart, tweenInfo, {Position = Vector3.new(PlrPosition)})

		tween:Play()
	end
end

see if this works

{Position = Vector3.new(PlrPosition)}{Position = PlrPosition}

Unforinitly this did not work…

@xXxOni_LordxXx Try this:

local ThisPart = script.Parent
local can = true

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math.random(1, #players)]

while wait() do
	local PlrPosition = pickedPlayer.Character:FindFirstChild("HumanoidRootPart").Position

	Distance = (ThisPart.Position - PlrPosition).Magnitude
	if Distance < 10 and can == true then

		can = false

		local TweenService = game:GetService("TweenService")

		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0,false,0)

		local tween = TweenService:Create(ThisPart, tweenInfo, {Position = PlrPosition})

		tween:Play()
	end
end

Improved by @GFink

I really appreciate it! There is one slight issue that I did not call for…

Is there any way to make it so the tween constantly moves to the player while the player walks?

I’m sorry that I did not call for this

Try calling the tween more than once as it does not yield the script.
Edit:

The Code
local ThisPart = script.Parent
local can = true

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math.random(1, #players)]

while wait() do
	local PlrPosition = pickedPlayer.Character:FindFirstChild("HumanoidRootPart").Position

	Distance = (ThisPart.Position - PlrPosition).Magnitude
	if Distance < 10 and can == true then
for count = 1,50 do
		can = false

		local TweenService = game:GetService("TweenService")

		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0,false,0)

		local tween = TweenService:Create(ThisPart, tweenInfo, {Position = PlrPosition})

		tween:Play()
wait(0.1)
end
	end
end

Don’t do it too much or it will lag

By putting the tween in the While wait() do?

You might want to check out my edit.

Remember if you do it too quickly it will lag. TweenService is costly.

So due to it being costly is there any other way I could come upon this?

The wait(0.1) shouldn’t cause too much lag, you should test it out to see if it lags and you can tween (no pun intended) the wait value to decrease lag.

Do you mind trying to fix the way the code is pasted in your comment? If not Idc just might take me a bit

I think this is a fixed format you can use:

local ThisPart = script.Parent
local can = true

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math.random(1, #players)]

while wait() do
	local PlrPosition = pickedPlayer.Character:FindFirstChild("HumanoidRootPart").Position

	Distance = (ThisPart.Position - PlrPosition).Magnitude
	if Distance < 10 and can == true then
		for count = 1,50 do
			can = false

			local TweenService = game:GetService("TweenService")

			local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0,false,0)

			local tween = TweenService:Create(ThisPart, tweenInfo, {Position = PlrPosition})

			tween:Play()
			wait(0.1)
		end
	end
end

robloxapp-20221127-1008209.wmv (1.8 MB)

These are the results and it seems to be very bugged
Sorry studio recording is very laggy

If it’s bugging it, just make it wait for the tween to finish for it to do another. (Set the wait time to the tween time)
Edit:

local ThisPart = script.Parent
local can = true

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math.random(1, #players)]

while wait() do
	local PlrPosition = pickedPlayer.Character:FindFirstChild("HumanoidRootPart").Position

	Distance = (ThisPart.Position - PlrPosition).Magnitude
	if Distance < 10 and can == true then
while Distance < 10 and can == true then
			-- can = false

			local TweenService = game:GetService("TweenService")

			local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0,false,0)

			local tween = TweenService:Create(ThisPart, tweenInfo, {Position = PlrPosition})

			tween:Play()
			wait(1) -- Tween Time.
		end
	end
end

Edit2: Fixed ugly code
Edit3: Fixed something LOL

I would not use a tween for this, actually. The issue with this code is that you’re creating a new 1 second tween every wait(), so you end up with a bunch of overlapping tweens. Even if you figured out a way to make it so only one tween happened at a time, it would not work correctly for a moving player, because the tween goes toward an unchanging plrPosition variable. This would make it so that if you trigger the orb while walking, it will move toward the position you triggered it at, and not the actual head position, which is gonna be constantly changing while you’re walking.

I assume you’re just trying to make it so you pick up the orb if you get near it. The easiest way to do this would just be to weld an invisible, CanCollide=false sphere to the orb whose hitbox is much bigger than the orb, then establish a .Touched connection on that hitbox sphere.

After they touch the sphere, initialize a RenderStepped connection where you manually reposition the orb every frame to give it the same gradual movement effect as a tween.

You got 2 options, go with Tweening, or with RenderStepping.

Do you think I could say if the distance is changed then cancel the tween and create a new one that is faster?

local Time = 1

Distance.Change:Connect(Function()
Time -= 0.1
If Time < 0.1 then
Time = 0.1
end
end)

1 Like

I got no idea how to do that
Like I got nothing

The distance will always change because the orb is always moving, and the character’s animations are causing the head to always have micro movements.