How to make a mouse input do a different thing based on variables?

Hello.

So, i’m making a grab knife script and mouse click 1 will do the grabbing animation. If the block infront of the grabber hits a humanoid, it welds the player onto the block infront of the grabber, and turning the “grabbed” variable into true.

The real problem arises when I try to script the killing, while the victim is grabbed.

I want the grab to happen with mouse click, and killing the victim too.

Basically, how do I change the mouse input based on the grabbed variable?

note; the touched:once(function(hit) is inside the first UIS.InputBegan:Connect(function(input)

Are you trying to block the input or do you mean something else?

I want to make it so that if the “grabbed” variable is true, then the mouseclick will do the “kill” function.

Here is my current structure:

UIS.InputBegan:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 then
					if not isgrabbing then
						print("Mouse click detected")
						clickSoundEvent:FireServer()
						grabtrack:Play() 
						isgrabbing = true
						local weldpart = player.Character:FindFirstChild("weldpart")
						weldpart.CanTouch = true
						weldpart.Touched:Once(function(hit)
							if hit.parent:FindFirstChild("HumanoidRootPart") and isgrabbing == true then
								isgrabbing = false
								grabbed = true
								if grabbed then
									print("Grabbed!")
									local victimroot = hit.parent:FindFirstChild("HumanoidRootPart")
									local weld = Instance.new("WeldConstraint")
									victimroot.CFrame = weldpart.CFrame
									weld.Parent = weldpart
									weld.Part0 = weldpart
									weld.Part1 = victimroot
									grabbedtrack:Play()
									UIS.InputBegan:Once(function(input)
										if input.UserInputType == Enum.UserInputType.MouseButton1 and grabbed then
											print("SLIT")
											slittrack:play()
										end
									end)
								end
							end
						end)
						
						task.wait(2) -- Adjust the delay as needed
						weldpart.CanTouch = false
						isgrabbing = false
					end
				end
			end)

Some parts are a bit messy because I haven’t gotten used to using tables for properties.

@iDertus7 Try this:

local function GetVictim(Part:BasePart)
	local Hitbox = workspace:GetPartsInPart(Part)
	
	local HRP = nil
	
	for I, X in Hitbox do
		local Parent = X.Parent
		local IsModel = Parent:IsA("Model")
		local Hum = Parent:FindFirstChildOfClass("Humanoid")
		
		if IsModel and Hum then
			HRP = Hum.RootPart
			break
		end
	end
	
	return HRP
end

UIS.InputBegan:Connect(function(input)
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	
	if isgrabbing then
		print("SLIT")
		slittrack:play()
		
	else	
		clickSoundEvent:FireServer()
		grabtrack:Play() 
		
		local weldpart = player.Character:FindFirstChild("weldpart")
		local victimroot = GetVictim(weldpart)

		if not victimroot then return end
		
		isgrabbing = true

		local weld = Instance.new("WeldConstraint")
		victimroot.CFrame = weldpart.CFrame
		weld.Parent = weldpart
		weld.Part0 = weldpart
		weld.Part1 = victimroot
		grabbedtrack:Play()
		
		task.wait(2) -- Adjust the delay as needed
		isgrabbing = false
	end
end)

Edit: If I didn’t get some parts right you can edit it to fit your needs because reading your code made me a bit confused

Hey. One problem that arises with getting the victim with the function is that when moving, it gets the grabbers humanoidrootpart.

I put print(HRP.Parent) inside the if IsModel and Hum then check and it printed my name when I was moving

Oh yea I forgot to filter the player character out. Here:

local function GetVictim(Part:BasePart, PlayerChar:Model)
	local Hitbox = workspace:GetPartsInPart(Part)
	
	local HRP = nil
	
	for I, X in Hitbox do
		local Parent = X.Parent
		local IsModel = Parent:IsA("Model")
		local Hum = Parent:FindFirstChildOfClass("Humanoid")
		
		if IsModel and Hum and Parent ~= PlayerChar then
			
			HRP = Hum.RootPart
			break
		end
	end
	
	return HRP
end
1 Like

Alright, I made minor adjustments to the layout of the code and now it works pretty well / the logic is now good to be built on.

Thank you for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.