How to make an animal eating system

hello, this is my first post so sry if im doing this wrong.
basically what I’m trying to do is make an animal eating system, since the characters of my game are all creatures. I’ve tried to find tutorials on this but all the tutorials I found make the character fill up when using a tool, but I dont know how to impliment this without a tool. I want to make it so the player has to be near the food object in order to eat it. here is my script:
local UserInputService = game:GetService(“UserInputService”)

Hi! welcome

I suggest you paste the code, so we can edit it faster, and use this button to format it
snap

Well, first, your while loop is constantly calling GetFuller() function, which is connecting over and over the InputBegan event. Theres no need to do that, once you connected it once the “Bind” is created and no need to do it again unless you disconnected it.

If you want the player only be able to eat while being near the food, you could read the distance between the player and the food and when is close enough turn a variable to true so the key press can refill hunger.
But I think it depends totally on details of your game. I mean. If you have lots of “foods” around the map, its not a good idea to iterate constantly all those elements to measure the distance to them…

So you could implement an invisible hitbox on touch, or use invisible proximityprompts to fire when a player gets close to that food and turn true the variables needed to eat

1 Like

oops, I tried to do that, but I dont see the little </> icon on my computer. sry!


local UserInputService = game:GetService(“UserInputService”)

local hungry = script.Parent
local player = game:GetService(“Players”).LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild(“Humanoid”)
local hunger = 1

local player = game:GetService(“Players”).LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild(“Humanoid”)

player.CharacterAdded:Connect(function()
local hunger = 1
hungry.Size = UDim2.new(hunger, 0,-1.001, 0)
end)

local function GetFuller()
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.E and hunger < .9 then
hunger = hunger + .1
wait(1)
else hunger = 1.02
end
end)

end

local function GetHungrier()
hunger = hunger - .01
hungry.Size = UDim2.new(hunger, 0,-1.001, 0)
wait(1)
end

while hunger > 0 do
GetFuller()
GetHungrier()
end


for a hitbox, is there a way to check if a certain thing is hitting the hitbox, or can the hitbox only check if it has something in it?

Instead of using local hunger, add a stats folder into your the player through SERVER using the .PlayerAdded thing. Then add an IntValue named Hunger inside of the stats folder and do all the hunger functions and stuff in that same .PlayerAdded thing. Now for EATING food, use a remote function.