Hi there,
I’m trying to make code that would magnetize a collectable towards the player once it touches the magnet’s hitbox. It seems easy enough, but I can’t figure out a way to get the collectable to cleanly glide towards the player, as the player would be constantly moving.
At first, I tried using tweens, but even with very fast tweens, it didn’t work well, since tweens can’t really constantly update the position it’s tweening to.
Here’s a snippet of the relevant code:
---On touch...
function onTouched(part)
if part.Name == "MagnetHitbox" and debounce == false then
debounce = true
local magTween = TweenService:Create(script.Parent, tweenInfo2 {Position = part.Parent.Position})
magTween:Play()
wait(0.3) --Waits until the tween ends
---Collect
local userName = part:FindFirstChild("User").Value --The "User" StringValue inside the magnet hitbox
local theuser = game.Players:FindFirstChild(userName)
local thescore = theuser:FindFirstChild("Bells") --"Bells" are the collectable
Collect(theuser, thescore)
debounce = false
end
end
script.Parent.Touched:connect(onTouched)
Next, I found out about LineForce. It was almost what I wanted, but I couldn’t find a way to only apply force to the collectable, so both the player and the collectable flung into each other.
Is there any other way I could go about doing this? Any help is greatly appreciated.