! Edible Food Script Help | [when tool is activated, it is removed from backpack]

Goal: When the player clicks/activates the food item, it is removed from their inventory.

My setup: This is located in lighting, I have a script so when the player clicks on a part, it clones + enters their inventory.

image

My script:
Control script below:

local remote = script.EatRemote

script.Parent.Activated:Connect(function()
	remote:FireServer("Eat")
end)

Eat script below:


remote.OnServerEvent:Connect(function(Player, value)
	local character = workspace:WaitForChild(Player.name)
	if value == "Eat" then
		script.Parent:Remove()
	end
end)

Errors:

Try using a WaitForChild on the line.

local remote = script:WaitForChild("EatRemote")

Thank you so much for the reply, I appreciate it! I put that in and it says this now, any possible ideas why? Should I have added wait()?
image

Or you could simply do script with:

script.Parent.Activated:Connect(function()
     script.Parent:Destroy()
end)

Also, I would not really suggest using :Remove() but :Destroy() would be better in this case

:WaitForChild() automatically waits until the child is added, thus wait() is not needed.

Also, in this case you are looking for “EatRemote” inside Bread.Control.Eat but the “EatRemote” is located in Bread.Control

1 Like

Ohh, okay! I added it but the red line + error popped up, any idea why? Thank you for the reply! :smiley:
image

Your function is still open - you need to close it with end)

So replace end with end)

my bad

1 Like

Ahhh! It worked, thank you so much, I really appreciate it a lot!! :smiley:

1 Like