I have a script that is supposed to Tween items to the player.
local TS = game:GetService("TweenService")
local function pickUpItem(v)
repeat wait(0) until (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - v.Position).Magnitude < 6
v.Anchored = true
v.CanCollide = false
local info = TweenInfo.new(0.15)
local newPos = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
local tween = TS:Create(v, info, {Position = newPos})
tween:Play()
tween.Completed:Wait()
local player = game.Players:GetPlayerFromCharacter(script.Parent)
v.Parent = player.Backpack
v.BackpackRustle:Play()
end
workspace.Drops.ChildAdded:Connect(function()
for i, v in pairs(workspace.Drops:GetChildren()) do
task.spawn(function()
pickUpItem(v)
end)
end
end)
Sometimes the Tween works, but at other times it just skips to the collecting part. What is going on? I’ve tried some things, but nothing works.
Your script is looping over every part when a new one is added when it should only get the new part that was added. This is most likely overlapping the functions.
Code:
local TS = game:GetService("TweenService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local info = TweenInfo.new(0.15)
local function pickUpItem(v)
repeat task.wait() until (LocalPlayer.Character.PrimaryPart.Position - v.Position).Magnitude < 6
v.Anchored = true
v.CanCollide = false
local newPos = LocalPlayer.Character.HumanoidRootPart.Position
local tween = TS:Create(v, info, {Position = newPos})
tween:Play()
tween.Completed:Wait()
v.Parent = LocalPlayer.Backpack
v.BackpackRustle:Play()
end
workspace.Drops.ChildAdded:Connect(pickUpItem)
for i, v in pairs(workspace.Drops:GetChildren()) do
task.spawn(pickUpItem, v)
end
Also, your code will not replicate to the server, because you’re doing this on a LocalScript. I’m not sure if that’s intended or not.
Now they’re all tweening, but parts with Welded parts/meshparts inside have a problem. The main part tweens, but the parts/meshpart inside doesn’t. Is there any way to get rid of this error?
Edit: I’ma try smth rq
Edit 2: it worked. sick. Thanks for the help!