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)
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.
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
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
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().
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)
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)