Detect whether the player changes their equipped item

This should work with any item btw.

I have a code that checks the item value and play the animation respectively according to the value but everytime the player switches item (no unequip) although the item value changes, the game does not know whether the value has changed or not (just does this if item.Value == “hi” then do stuff like play anim) and if it changes it does nothing and then the past animation stays

(Posted this topic hours ago but no correct answer yet. TL;DR : Check if player switches their equipped item without unequipping it)

3 Likes

I think that we’d have to see your code to know what the problem with it is. However, if you’re using the default tool system, you should be able to use ContextActionService.LocalToolEquipped on the client or you can check when a Tool gets added to the player’s character.

Because you can play animations from the client, and those animations will replicate to the server, you should be able to use ContextActionService.LocalToolEquipped and ContextActionService.LocalToolUnequipped to determine animations.

I dont want it to replicate to the server. It is a viewmodel system

Here is a piece of my code

local function isHoldingTool(player)
	local ch = player.Character or player.CharacterAdded:Wait()
	if ch:FindFirstChildWhichIsA("Tool") then
		local tool = ch:FindFirstChildWhichIsA("Tool")
		hold = true
		if hold == true then
			if debounce == false  then
				item = game.ReplicatedStorage.Viewmodels.Item:WaitForChild(tool.Name):Clone()
				MAINMODULE.welditem(item)
				MAINMODULE.equiq(ViewModel, item)
				if tool.Value.ViewModelValue.Value == "Flashlight" then
					open:Play()
					debounce = true
					open.Stopped:Wait()
					for _, v in pairs(ViewModel.RightArm:GetDescendants()) do
						if v:IsA("BasePart") then
							v.Transparency = 0
						end
					end
					Hold:Play()
				elseif tool.Value.ViewModelValue.Value == "Normal" then
					open:Play()
					debounce = true
					open.Stopped:Wait()
					for _, v in pairs(ViewModel.RightArm:GetDescendants()) do
						if v:IsA("BasePart") then
							v.Transparency = 0
						end
					end
					Hold:Play()
				end
			end
		end
	else
		hold = false
		if hold == false then
			if #ViewModel.RightArm:GetChildren() == 0 then
				return false
			else 
				for i, v in pairs(ViewModel.RightArm:GetChildren()) do
				  	v:Destroy()
				end
			end
			if debounce then
				Hold:Stop()
				debounce = false
			end
			for _, v in pairs(ViewModel.RightArm:GetDescendants()) do
				if v:IsA("BasePart") then
					v.Transparency = 1
				end
			end
		end
	end

I want Hold animation and Open to stop if the player switches item and then let it play other animation needed

1 Like

I have to go, so I cannot finish helping you right now. But, in case anyone else is curious, how are you detecting when to call this function?

The problem could also be with your debounce. You’re checking if debounce is false before moving on to playing the animations, and you only set it back to false when an item is unequipped. So, that could contribute to your struggle.

1 Like

debounce is to remove the infinite loop when playing animation since am gonna keep checking,

this function is always running to check if player is holding an item

just need to know whether the player switches item without unequipping rn and then i can do the rest that one there is just an example :smile:

When a player equips an item, that item is removed from their Backpack and added to their Character. Using this logic, you can determine if they were holding an item beforehand.

1 Like

Hey, Just understood what you meant about the debounce part, Thanks!

Thanks, currently making a part with this logic. I’ll let you know If I need any assistance