Why isn't my food script working?

So, basically I’m just trying out a simple hunger system. The issue is coming in that my activated isn’t working.


local Toolie = script.Parent

Toolie.Activated:Connect(function()

	if Debounce == true then return end

	if Debounce == false then
		Debounce = true 
		print("mhm")

		RS.Eating:FireServer()
		
		task.wait(9)

		Debounce = false

	end



end)

So, then what comes out of that is my question, I tried changing the name, making an independent variable for the tool, but it doesn’t really assist me in any way. The actual script works, as I did a task.wait(20) and the hunger went back up as if the character ate something in the last 20 seconds.

image

It’s located in the starterpack, so it’s very confusing to me as of why it’s not working.

Are you ever defining “debounce”? If not, it’s equal to nil, therefore none of your if-statements’ conditions are ever true. You don’t even need to check if debounce == false because you’re returning if it’s true, never which would result in never continuing the function if the debounce was true.

-- \\ Player-Related Variables //--

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HUM = Character:WaitForChild("Humanoid")
local Humrp = Character:WaitForChild("HumanoidRootPart")

-- \\ Get-Service Variables // -- 

local UIS = game:GetService("UserInputService")

local RS = game:GetService("ReplicatedFirst")

-- \\ Server-Script-Variables // --

local SSS = game:GetService("ServerScriptService")

local RS = game:GetService("ReplicatedStorage")

local RunS = game:GetService("RunService")

local SP = game:GetService("StarterPack")

local CAS = game:GetService("ContextActionService")

local Debounce = false

Everything I believe is properly defined.

Why are you defining “RS” twice? At the start you define it as ReplicatedFirst and then as ReplicatedStorage?

Oh, I think this was an older script in which I just copy and pasted through out various other scripts. Shoud probably check up on that.

To to really know if the activated event isn’t working, try printing after the event is connected

You can try this here

--//Local Script

local tool = script.Parent
local debounce = false

local debounceHandler = function()
	debounce = true
	task.wait(9)
	debounce = false
end

tool.Activated:Connect(function()
	if not debounce then
		task.spawn(debounceHandler)
		
		game:GetService('ReplicatedStorage').Eating:FireServer()
	end
end)

--//Server Script
game:GetService('ReplicatedStorage').Eating.OnServerEvent:Connect(function(player)
	print('Event was fired from player '..player.Name)
end)

Won’t this wait 9 seconds before the remote is fired? I think he wants the debounce to last for 9 seconds before it fires again.

It is task.spawn so therefore the code will not yield.

Thanks for explaining, this makes sense.

Even after using this, the function still isn’t working.

Did you trying printing as soon as the tool activates?

tool.Activated:Connect(function()
	print("Activated")
	if not debounce then
		task.spawn(debounceHandler)

		game:GetService('ReplicatedStorage').Eating:FireServer()
	end
end)

Yup, and there is simply no print in the output.

Does the tool have a part named Handle? If you’re not using a handle in the tool, make sure the RequiresHandle property is checked set to false.
image

Yup, made it be like that.
image

If it didn’t print immediately after .Activated you should put a print right before .Activated to make sure you haven’t accidentally created some logic that will never reach it.

--//Local Script

local tool = script.Parent
local debounce = false

print("localtestingprior")

local debounceHandler = function()
	debounce = true
	task.wait(9)
	debounce = false
end

print("testing")

tool.Activated:Connect(function()
	if not debounce then
		print("were we working before")
		task.spawn(debounceHandler)
		print("spawn")
		game:GetService('ReplicatedStorage').Eating:FireServer()
		print("we on?")
	end
end)

It’ll only print anything outside of the .Activated. So even with this information I’m still confused on what the issue could be.

Ahh. Set ManualActivationOnly to false. When it’s true Activate will only fire when you call tool:Activate() in a script. It will ignore mouse input when that’s true.