So I have this script that deletes an ingredient upon collecting it, and I was looking for ways so errors wouldn’t happen. I found out Debris was a good service that works better than :Destroy(), so I used it. For some reason I keep getting the same error:
Players.imabossxd123456.Backpack.SuperMagnet.LocalScript:37: attempt to index nil with 'Parent'
Here is my code it:
UpdateClient.OnClientEvent:Connect(function(Ingredient)
if Ingredient == "Carrot" then
for i, v in pairs(workspace:GetChildren()) do
if v.Name == "Carrot" then
if v.Container:FindFirstChild("plrInstance") then
local CarrotValue = v.Container:FindFirstChild("plrInstance")
if CarrotValue and CarrotValue.Value == plr.Name then
local HRP = plr.Character.HumanoidRootPart
local tween = TS:Create(CarrotValue.Parent, info, {CFrame = CFrame.new(HRP.Position)})
tween:Play()
tween.Completed:Wait()
Debris:AddItem(CarrotValue.Parent.Parent, 0.2)
end
end
end
end
end
end)
Basically what’s happening is I put a string value in each of the models, then looked to see if it found that players name in it, if it does, then I destroy it.
It would be helpful to have the whole script, especially because I can’t tell what line it actually errored on.
My best guess is carrotvalue.Parent doesn’t exist. But I can’t say for certain.
Oh yeah I completely forgot about the other parts here is the entire script:
local Magnet = script.Parent
local back = Magnet.M.Back
local Front = Magnet.M.Front
local Speed = 15
local Equip = game:GetService("ReplicatedStorage").MagnetEquip
local TS = game:GetService("TweenService")
local info = TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local UpdateClient = game:GetService("ReplicatedStorage").UpdateClient
local plr = game.Players.LocalPlayer
local Debris = game:GetService("Debris")
Magnet.Equipped:Connect(function()
--plr.PlayerScripts.ORBITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT.Trail.Enabled = true
end)
UpdateClient.OnClientEvent:Connect(function(Ingredient)
if Ingredient == "Carrot" then
for i, v in pairs(workspace:GetChildren()) do
if v.Name == "Carrot" then
if v.Container:FindFirstChild("plrInstance") then
local CarrotValue = v.Container:FindFirstChild("plrInstance")
if CarrotValue and CarrotValue.Value == plr.Name then
local HRP = plr.Character.HumanoidRootPart
local tween = TS:Create(CarrotValue.Parent, info, {CFrame = CFrame.new(HRP.Position)})
tween:Play()
tween.Completed:Wait()
Debris:AddItem(CarrotValue.Parent.Parent, 0.2)
end
end
end
end
end
end)
task.spawn(function()
while true do
for i = 0,1,0.001*Speed do
back.Color = Color3.fromHSV(i, 1, 1)
wait()
end
end
end)
That was my guess as well, but when I was reading debris:AddItem() it said
Debris does not yield the current thread, does not require a new thread and will not error if the object is already destroyed. For this reason it is the recommended method for cleaning up objects with a fixed lifetime.
local Magnet = script.Parent
local back = Magnet.M.Back
local Front = Magnet.M.Front
local Speed = 15
local Equip = game:GetService("ReplicatedStorage").MagnetEquip
local TS = game:GetService("TweenService")
local info = TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local UpdateClient = game:GetService("ReplicatedStorage").UpdateClient
local plr = game.Players.LocalPlayer
local Debris = game:GetService("Debris")
Magnet.Equipped:Connect(function()
--plr.PlayerScripts.ORBITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT.Trail.Enabled = true
end)
UpdateClient.OnClientEvent:Connect(function(Ingredient)
if Ingredient == "Carrot" then
for i, v in pairs(workspace:GetChildren()) do
if v.Name == "Carrot" then
if v.Container:FindFirstChild("plrInstance") then
local CarrotValue = v.Container:FindFirstChild("plrInstance")
if CarrotValue and CarrotValue.Value == plr.Name then
local HRP = plr.Character.HumanoidRootPart
local tween = TS:Create(CarrotValue.Parent, info, {CFrame = CFrame.new(HRP.Position)})
tween:Play()
tween.Completed:Wait()
Debris:AddItem(v.Parent, 0.2)
end
end
end
end
end
end)
task.spawn(function()
while true do
for i = 0,1,0.001*Speed do
back.Color = Color3.fromHSV(i, 1, 1)
wait()
end
end
end)
Ok so I just tested it out and it’s still there, but when collecting multiple at the same time, the errors starts to occur. If I just collect 1, it doesn’t error.
Would you also like to see the server script? My goal here was to make a magnet that collects, but only renders on the client so op players can’t ruin the game for new players.
Like maybe if I did something wrong on the server I don’t know tbh. I should probably mention this is on a .Touched event so maybe since it fires multiple times it errors out?