Prevent switching to other tools while animation is active

Hey everyone!! I’m trying to make a hunger system where you can eat food to increase your hunger which if you don’t you’ll eventually die… anyways, whenever the eating animation is playing, you can switch to another tool making it so the animation plays while that tool is equipped, which can make it look rather silly.

Here’s what I mean:
https://i.gyazo.com/69b6226f9a62babb1f8d7b9b7202711a.gif

Is there a way I can prevent switching to another tool while an animation is playing or is there a way I can stop the animation if the tool gets unequipped? Any help is appreciated, thank you so much!!

2 Likes

This

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

Can only run from a local script, when ran will not let you switch tool while also not letting you unequip the current tool on hand.
Utilize it however you want.

3 Likes

Stopping a tool’s animation when that tool is unequipped is as simple as calling :Stop() on the animation track instance when the tool’s .Unequipped event fires.

local tool = script.Parent

tool.Unequipped:Connect(function()
	--Stop animation here.
end)

Preventing a tool from being unequipped while its animation is played without outright disabling the core backpack gui requires a little extra code.

local tool = script.Parent
local track --Define track here.

tool.Unequipped:Connect(function()
	if track.IsPlaying then --Checks if the animation track is playing.
		local player = tool:FindFirstAncestorOfClass("Player")
		if player then
			local character = player.Character
			if character then
				local humanoid = character:FindFirstChildOfClass("Humanoid")
				if humanoid then
					humanoid:EquipTool(tool) --Re-equip tool.
				end
			end
		end
	end
end)
2 Likes