Hello all, I have made a flooring part that, when touched, should give a magnetic effect to the player which attracts all nearby bananas on their banana farm. It does this by making a line force for each banana found and attaching it to one of the attachments on the character rig.
This mostly works just as intended, but after the banana reaches the player, it seems to get violently flung in a random direction. Each banana eventually gets collected, but it doesn’t seem to happen until after it gets flung like this (Each banana has its own script dealing with collisions for the player to collect it).
I have each banana in a collision group that makes them not collide with characters, so my first thought was that the force on the banana approaches infinity as it gets closer to the torso (I have inversesquarelaw as true to have the magnetic effect), so I should make each banana collidable with the character so that it doesn’t go inside of the torso and get a distance of less than 1. This seems to have only made the problem worse, and I really don’t know what to do now. I’m not sure if I’m doing something wrong with the line force or if there is even a real solution to this problem at all, but any solution would be greatly appreciated. Thank you
local DB = false
local magnetizedPlayers = {}
function magnetize(part)
if part and part.Parent and part.Parent:FindFirstChild("Humanoid") and DB == false then
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player and player.TeamColor == script.Parent.Parent.Parent:GetAttribute("Team") then
if not magnetizedPlayers[player.Name] then
DB = true
for i,v in pairs(script.Parent.Parent.Parent:GetChildren()) do
if v.Name:match("Banana") then
local magnet = Instance.new("LineForce")
magnet.Attachment0 = v.Attachment
magnet.Attachment1 = player.Character.UpperTorso.BodyFrontAttachment
magnet.InverseSquareLaw = true
magnet.Parent = part.Parent.HumanoidRootPart
game:GetService("Debris"):AddItem(magnet,30)
end
end
magnetizedPlayers[player.Name] = player
DB = false
end
end
elseif part and part:FindFirstChild("Eat") and DB == false then
DB = true
for i,v in pairs(magnetizedPlayers) do
if v.Character then
local magnet = Instance.new("LineForce")
magnet.Attachment0 = part.Attachment
magnet.Attachment1 = v.Character.UpperTorso.BodyFrontAttachment
magnet.InverseSquareLaw = true
magnet.Parent = v.Character.HumanoidRootPart
game:GetService("Debris"):AddItem(magnet,30)
end
end
DB = false
end
end
script.Parent.Touched:Connect(magnetize)