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)
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.
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
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