Touched event does not work

I tried changing properties and stuff already

image

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
1 Like

food isn’t a valid parameter for a hit event, the hit parameter is perfectly fine though!

quote=“NaturalPlants, post:1, topic:2466996, username:NaturalPlants”]
food.Touched:Connect(onTouched,food)
[/quote]

This also isn’t a valid way to call a function.

You’ll need to do something like this:

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

For some reason it still does not work. I have tried printing, but it shows nothing at all.

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. :slight_smile:

1 Like

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.

Are there any errors logged in the console?

What are you using to clone and place the food objects into WorkspaceFood – is it a LocalScript?

It shows no errors. I’m using server script to clone and parent them to WorkspaceFood folder

Okay, you could use this code and it should work:

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:

print(#CS:GetTagged('Food'))

Hope this helped. :slight_smile:

1 Like

Thank you. Now the touched event works, but it throws an error which says that it couldn’t find Hunger value.

No worries — is your “Hunger” Value actually in leaderstats? If so, change to
plr.leaderstats.Hunger.Value += 10

If not, paste the code for when you create the Hunger value. :slight_smile:

Oh, i forgot to reference leaderstats. Now everything works fine. Thank you for the help.

Can you explain how this code works? If you can.

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. :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.