Pickupscript not working?

When I was developing, I ran into this problem:

this is what I was doing:

This is my local script:

local CS = game:GetService("CollectionService")
local CollectionFood = CS:GetTagged("CollectionParts")
local CollectionParts = CS:GetTagged("CollectionFood")
local Hunger = game:GetService("ReplicatedStorage"):WaitForChild("Hunger")
local PickUp = game:GetService("ReplicatedStorage"):WaitForChild("PickUp")
local UIS = game:GetService("UserInputService")

for _, Part in pairs(CollectionParts) do
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				PickUp:FireServer(Reference)
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
	end)
end

for _, Part in pairs(CollectionFood) do
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				print(Part)
				print(Part.Parent)
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				PickUp:FireServer(Reference)
			end
			if key.KeyCode == Enum.KeyCode.E then
				local HungerAdd = Part.Hunger.Value
				Part:Destroy()
				Hunger:FireServer(HungerAdd)
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
	end)
end

and this is my server script:

local Hunger = game:GetService("ReplicatedStorage"):WaitForChild("Hunger")
local PickUp = game:GetService("ReplicatedStorage"):WaitForChild("PickUp")

Hunger.OnServerEvent:Connect(function(player, HungerAdding)
	local Stats = player:WaitForChild("Stats")
	if Stats ~= nil then
		local Hunger = Stats:WaitForChild("Hunger")
		if Hunger ~= nil then
			Hunger.Value = Hunger.Value + HungerAdding
			if Hunger.Value > 100 then
				Hunger.Value = 100
			end
		end
	end
end)

Hunger.OnServerEvent:Connect(function(player, Material)
	local Stats = player.Stats
    if Stats ~= nil then
        if Stats:FindFirstChild(Material) then
            Stats:FindFirstChild(Material).Value = Stats:FindFirstChild(Material).Value + 1
        end
    end
end)

here are the locations:

Capture

all help is greatly appreciated.

1 Like

You’ll need to remove the part from the CollectionParts/CollectionFood table after destroying it, otherwise your script will still act like it exists and error.

1 Like

how do I do that? 30charslolol

You’ll need to set the index of the table containing the part to nil.
An example of this added to your code:

local CS = game:GetService("CollectionService")
local CollectionFood = CS:GetTagged("CollectionParts")
local CollectionParts = CS:GetTagged("CollectionFood")
local Hunger = game:GetService("ReplicatedStorage"):WaitForChild("Hunger")
local PickUp = game:GetService("ReplicatedStorage"):WaitForChild("PickUp")
local UIS = game:GetService("UserInputService")

for _, Part in pairs(CollectionParts) do
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionParts[index] = nil
				PickUp:FireServer(Reference)
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
	end)
end

for index, Part in pairs(CollectionFood) do
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				print(Part)
				print(Part.Parent)
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionFood[index] = nil
				PickUp:FireServer(Reference)
			end
			if key.KeyCode == Enum.KeyCode.E then
				local HungerAdd = Part.Hunger.Value
				Part:Destroy()
				CollectionFood[index] = nil
				Hunger:FireServer(HungerAdd)
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
	end)
end
1 Like

wont that stop the tag from working?

Now I have this problem:


the index part is fine, I fixed it, but the parent one is really confusing

It’ll only make the script ignore the parts you’ve already collected/eaten.

1 Like

You aren’t disconnecting the UserInputService events once you’ve collected/eaten the parts either. Not only could this cause a memory leak, but it’s also causing it to error.

Try this:

local CS = game:GetService("CollectionService")
local CollectionFood = CS:GetTagged("CollectionParts")
local CollectionParts = CS:GetTagged("CollectionFood")
local Hunger = game:GetService("ReplicatedStorage"):WaitForChild("Hunger")
local PickUp = game:GetService("ReplicatedStorage"):WaitForChild("PickUp")
local UIS = game:GetService("UserInputService")

for index, Part in pairs(CollectionParts) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		inputConnection = UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionParts[index] = nil
				PickUp:FireServer(Reference)
			end
		end)
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
end

for index, Part in pairs(CollectionFood) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		inputConnection = UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				print(Part)
				print(Part.Parent)
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionFood[index] = nil
				PickUp:FireServer(Reference)
			end
			if key.KeyCode == Enum.KeyCode.E then
				local HungerAdd = Part.Hunger.Value
				Part:Destroy()
				CollectionFood[index] = nil
				Hunger:FireServer(HungerAdd)
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
end
1 Like

Everything works but the eating part, it doesnt get destroyed or anything.

That might be because it only destroys the part itself rather than the part’s parent on line 51, unlike the rest of the script.
Try replacing line 51 with Part.Parent:Destroy().

1 Like

it’s not that, it’s that it doesnt even pass the if statement for the e detect(i tested it with print)

Could you send a screenshot of the output after you’ve pressed E with the following code:

Code
local CS = game:GetService("CollectionService")
local CollectionFood = CS:GetTagged("CollectionParts")
local CollectionParts = CS:GetTagged("CollectionFood")
local Hunger = game:GetService("ReplicatedStorage"):WaitForChild("Hunger")
local PickUp = game:GetService("ReplicatedStorage"):WaitForChild("PickUp")
local UIS = game:GetService("UserInputService")

for index, Part in pairs(CollectionParts) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		inputConnection = UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionParts[index] = nil
				PickUp:FireServer(Reference)
			end
		end)
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
end

for index, Part in pairs(CollectionFood) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		inputConnection = UIS.InputBegan:Connect(function(key)
			print('Pressed key:',key.KeyCode)
			if key.KeyCode == Enum.KeyCode.C then
				print('Pressed C')
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionFood[index] = nil
				PickUp:FireServer(Reference)
			end
			if key.KeyCode == Enum.KeyCode.E then
				print('Pressed E')
				local HungerAdd = Part.Hunger.Value
				Part.Parent:Destroy()
				CollectionFood[index] = nil
				Hunger:FireServer(HungerAdd)
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
end

(Basically just adds print debugging to the InputAdded function)

1 Like

First time jump: pressed c
second time and after that: pressed e
last time: pressed c


Okay, so I (hopefully) figured out what was going on…
You mixed up the variable names of CollectionFood and CollectionParts at the beginning of the script.

Corrected Script
local CS = game:GetService("CollectionService")
local CollectionParts = CS:GetTagged("CollectionParts")
local CollectionFood = CS:GetTagged("CollectionFood")
local Hunger = game:GetService("ReplicatedStorage"):WaitForChild("Hunger")
local PickUp = game:GetService("ReplicatedStorage"):WaitForChild("PickUp")
local UIS = game:GetService("UserInputService")

for index, Part in pairs(CollectionParts) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		inputConnection = UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionParts[index] = nil
				PickUp:FireServer(Reference)
			end
		end)
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
end

for index, Part in pairs(CollectionFood) do
	local inputConnection
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		inputConnection = UIS.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.C then
				local Reference = Part.Parent.Name
				Part.Parent:Destroy()
				CollectionFood[index] = nil
				PickUp:FireServer(Reference)
			end
			if key.KeyCode == Enum.KeyCode.E then
				local HungerAdd = Part.Hunger.Value
				Part.Parent:Destroy()
				CollectionFood[index] = nil
				Hunger:FireServer(HungerAdd)
			end
		end)
	end)
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		Part.Instructions.Enabled = false
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
	Part.AncestryChanged:Connect(function()
		if inputConnection then
			inputConnection:Disconnect()
			inputConnection = nil
		end
	end)
end

(If this doesn’t work, you’ll need someone else to help you with this. my brain hurts)

2 Likes