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
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
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
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
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.
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
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
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.