local WorkspaceFood = game.Workspace.WorkspaceFood
local PlaySoundRE = game.ReplicatedStorage.RemoteEvents.PlaySoundRE
local CS = game:GetService("CollectionService")
local function onTouched(hit,food)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
local char = Humanoid.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
plr:FindFirstChild("Hunger").Value += 10
PlaySoundRE:FireClient(plr)
food:Destroy()
end
end
for i,food in pairs(CS:GetTagged("Food")) do
if not food then return end
food.Touched:Connect(onTouched,food)
end
local WorkspaceFood = game.Workspace.WorkspaceFood
local PlaySoundRE = game.ReplicatedStorage.RemoteEvents.PlaySoundRE
local CS = game:GetService("CollectionService")
local function onTouched(hit,food)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
local char = Humanoid.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
plr:FindFirstChild("Hunger").Value += 10
PlaySoundRE:FireClient(plr)
food:Destroy()
end
end
for i,food in pairs(CS:GetTagged("Food")) do
if not food then return end
food.Touched:Connect(function(hit)
onTouched(hit,food)
end)
end
Touched is only possible for BaseParts. It is also possible that the event will fire more than 1 time before the food is removed, I recommend taking this into account.
local WorkspaceFood = game.Workspace.WorkspaceFood
local PlaySoundRE = game.ReplicatedStorage.RemoteEvents.PlaySoundRE
local CS = game:GetService("CollectionService")
local function onTouched(hit,food)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
local char = Humanoid.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
plr:FindFirstChild("Hunger").Value += 10
PlaySoundRE:FireClient(plr)
food:Destroy()
end
end
for i,food in pairs(CS:GetTagged("Food")) do
if not food then return end
for _, v in pairs(food:GetChildren()) do
if not v:IsA("BasePart") then continue end
v.Touched:Connect(function(hit)
onTouched(hit, food)
end)
end
end
Are you sure you’ve tagged the food items “Food” or did you just tag the folder that contains the items as “Food”? Make sure you’ve given each part the food tag.
I did tag every food part. I tried doing touched event on a simple brick ,it seem to work fine, but it doesn’t work on food parts. I have tried not tagging , but using GetChildren() and it did not work too. I do not know what is the problem.
local WorkspaceFood = workspace.WorkspaceFood
local PlaySoundRE = game.ReplicatedStorage.RemoteEvents.PlaySoundRE
local CS = game:GetService("CollectionService")
local Foods = {}
local function onTouched(hit,food)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
Foods[food].Debounce = true
plr:FindFirstChild("Hunger").Value += 10
PlaySoundRE:FireClient(plr)
Foods[food].Connection:Disconnect()
Foods[food] = nil
food:Destroy()
end
end
local SetupFood = function(object)
if object:IsA('BasePart') and object:HasTag('Food') then
if not Foods[object] then
Foods[object] = {Debounce = false}
Foods[object].Connection = object.Touched:Connect(function(hit)
if not Foods[object].Debounce then
onTouched(hit,object)
end
end)
end
end
end
WorkspaceFood.ChildAdded:Connect(function(object)
SetupFood(object)
end)
for i,food in pairs(CS:GetTagged("Food")) do
SetupFood(food)
end
If that doesn’t work, I would say to add this line and see what the output is:
No worries – please mark mine as solution so others know this has been answered.
As for the code and how it works in the SetupFood function, we check that the object passed is a BasePart and has the tag of “Food”. If so, we check that the Food part hasn’t already been added to the dictionary we use for storing the Touched connection for each food. Upon when the food part is touched, we check that the part hasn’t already been touched by using the Debounce key. If it hasn’t been touched, we call onTouched().
Inside the onTouched() function, we get the player that touched it, if it was a player, we continue. We set Debounce so that when / if the part is touched again, it won’t call the onTouched() function. We set the Hunger value, Fire the RemoteEvent, Disconnect the Touched connection since it’ll no longer be needed, and set the dictionary field for food to nil to clear it out, lastly, destroy the part.
Hopefully that wasn’t too long-winded and was what you were asking about, haha.