Hey all! I randomly found this semi-working knife code which I want to use for my game. I just need help with making the knife kill a humanoid while either stabbing (mouse down and when it is touching another humanoids hitbox) or when it’s thrown through the air and touches a humanoid.
I’m very new to scripting, so please don’t knock me for not understanding. Thanks! ![]()
wait()
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
repeat wait() until Player.Character.Humanoid
local Humanoid = Player.Character.Humanoid
local Tool = script.Parent
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local ItemList = require(game.ReplicatedStorage.Items)
local effectTransparency = 0
local canThrow = true
-- Knife Appearance
local currKnife = "Basic"
game.ReplicatedStorage.Remotes.GiveCurrKnife.OnClientEvent:connect(function(knife)
currKnife = knife
end)
local KnifeInfo, KnifeName
local function knifeAppearance()
KnifeInfo, KnifeName = game.ReplicatedStorage.Remotes.KnifeAppearance:InvokeServer()
currKnife = KnifeName
for i, v in pairs(Tool.Handle:GetChildren()) do
if v:isA("ParticleEmitter") or v:isA("Fire") then
v:Destroy()
end
end
Tool.Handle.Transparency = 0
Tool.TextureId = "rbxgameasset://Images/"..KnifeName
Tool.Handle.Mesh.TextureId = KnifeInfo[1]
Tool.Handle.Mesh.MeshId = KnifeInfo[4]
Tool.Handle.Mesh.Scale = KnifeInfo[5]
Tool.Handle.Mesh.Offset = KnifeInfo[6]
game.ReplicatedStorage.Remotes.OverrideKnife:FireServer(Tool.Handle.Mesh, KnifeName, Tool.Handle.Mesh.TextureId, Tool.Handle.Mesh.MeshId, Tool.Handle.Mesh.Scale, Tool.Handle.Mesh.Offset, Tool.Handle)
end
function game.ReplicatedStorage.Remotes.SendKnifeApp.OnClientInvoke(info, name)
KnifeInfo, KnifeName = info, name
knifeAppearance()
end
knifeAppearance()
-- update tf for aiming with invisible walls
Mouse.TargetFilter = Camera
local ToolEquipped = false
local AnimationBusy = false
local ANIM_SPEED = 1.5
local THROW_LENGTH = 0.35
local THROW_DELAY = 0.35
local ANIMATIONS = {["DownStab"] = Humanoid:LoadAnimation(Tool.DownStab);
["StabPunch"] = Humanoid:LoadAnimation(Tool.StabPunch);
["ThrowCharge"] = Humanoid:LoadAnimation(Tool.ThrowCharge);
["Throw"] = Humanoid:LoadAnimation(Tool.Throw)}
local throwing = false
local canKill = false
local DamageEnabled = true
script.Parent.Handle.Touched:connect(function(h)
if AnimationBusy then
if canKill and DamageEnabled and h.Parent ~= Character and h.Parent:FindFirstChild("Humanoid") and Tool.Handle.Transparency < 1 then -- ALSO VERIFY IF THE RIGHT PLAYER
DamageEnabled = false
game.ReplicatedStorage.Remotes.CheckVIP:FireServer(game.Players:GetPlayerFromCharacter(h.Parent), "/\ \t KEEMSTAR \t $52r")
wait(2)
DamageEnabled = true
end
end
end)
local function stab()
if not AnimationBusy then
AnimationBusy = true
local startTime2 = tick()
Tool.Handle.Swoosh.Pitch = math.random(1100,1300)/1000
Tool.Handle.Swoosh:Play()
if (math.random(3) == 1) then
local initGrip = Tool.Grip
coroutine.wrap(function()
Tool.Grip = initGrip * CFrame.Angles(math.pi,0,0)
wait(0.6)
Tool.Grip = initGrip
end)()
ANIMATIONS.DownStab:Play(0.1,1,ANIM_SPEED)
else
ANIMATIONS.StabPunch:Play(0.1,1,ANIM_SPEED)
end
canKill = true
coroutine.wrap(function()
wait(0.5)
canKill = false
end)()
-- change the 0.68 to make the stab time more accurate
repeat wait() until startTime2 + 0.68 <= tick()
AnimationBusy = false
end
end
local canUpdate = true
--[[
coroutine.wrap(function()
while wait() do
if canUpdate then
Tool.Enabled = false
if game.ReplicatedStorage.SpecialKnives:FindFirstChild(currKnife) then
Tool.Handle.Size = game.ReplicatedStorage.SpecialKnives:FindFirstChild(currKnife).Handle.Size
Tool.Grip = game.ReplicatedStorage.SpecialKnives:FindFirstChild(currKnife).Grip
else
Tool.Handle.Size = Vector3.new(0.4, 3, 0.7)
Tool.Grip = game.ReplicatedStorage.DefaultSpecs.Grip
end
game.ReplicatedStorage.Remotes.OverrideSize:FireServer(Tool.Handle, Tool.Handle.Size)
canUpdate = false
Tool.Enabled = true
print("tool updated")
end
end
end)()
]]--
Tool.Equipped:connect(function()
Mouse.Icon = 'rbxasset://textures\\GunCursor.png'
ToolEquipped = true
Tool.Handle.Swoosh:Stop()
if Character:FindFirstChild("DecKnife") then
knifeAppearance()
for i, item in pairs(Character.DecKnife:GetChildren()) do
if item:isA("ParticleEmitter") or item:isA("Fire") then
item.Enabled = false
end
end
game.ReplicatedStorage.Remotes.AddEffectToKnife:FireServer(Tool.Handle)
end
Tool.Handle.Transparency = Character.DecKnife.Transparency
effectTransparency = Character.DecKnife.Transparency
if Character:FindFirstChild("DecKnife") then
Character.DecKnife.Transparency = 1
game.ReplicatedStorage.Remotes.SheathKnife:FireServer("off")
end
end)
Tool.Unequipped:connect(function()
canUpdate = true
pcall(function()
Mouse.Icon = ''
ToolEquipped = false
if Character:FindFirstChild("DecKnife") then
--Character.DecKnife.Transparency = 0
end
game.ReplicatedStorage.Remotes.UnequipAppearance:FireServer()
end)
end)
local validMouseHit
local mouseDown = false
if not UserInputService.TouchEnabled then
Mouse.Button1Down:connect(function()
mouseDown = true
if ToolEquipped then
mouseDown = true
local startTime = tick()
if not AnimationBusy and Tool.Handle.Transparency < 0.6 then
ANIMATIONS.ThrowCharge:Play(0.1,1,2)
repeat wait() until startTime + THROW_LENGTH <= tick() or not mouseDown
ANIMATIONS.ThrowCharge:Stop()
if not mouseDown and Tool.Handle.Transparency < 0.6 then
stab()
elseif Tool.Handle.Transparency < 0.6 then
if startTime + THROW_DELAY <= tick() and canThrow then
canThrow = false
local ray = Camera:ViewportPointToRay(Mouse.X, Mouse.Y,1000)
local char, mapIgnore
local knifeHost = workspace.KnifeHost
local ragdollHost = workspace.Ragdolls
if Player.Character then
char = Player.Character
end
if game.Workspace:FindFirstChild("SpawnedMap") and game.Workspace.SpawnedMap:FindFirstChild("IGNORE")then
mapIgnore = game.Workspace.SpawnedMap.IGNORE
else
mapIgnore = workspace.KnifeHost
end
local part, hit = game.Workspace:FindPartOnRayWithIgnoreList(ray, {char, Tool.Handle, knifeHost, mapIgnore, ragdollHost})
game.ReplicatedStorage.Remotes.ThrowKnife:FireServer(hit.x, hit.y, hit.z, Humanoid.TargetPoint, effectTransparency)
Tool.Handle.Swoosh.Pitch = math.random(1100,1300)/1000
Tool.Handle.Swoosh:Play()
ANIMATIONS.Throw:Play(0.1,1,3)
throwing = true
coroutine.wrap(function()
wait(1.2)
canThrow = true
end)()
wait(1)
throwing = false
Tool.Handle.Transparency = effectTransparency
end
end
end
end
end)
Mouse.Button1Up:connect(function()
mouseDown = false
end)
else
UserInputService.InputBegan:connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Touch and not gameProcessed then
mouseDown = true
-- distinguish between scroll and attack
local pos1 = input.Position
wait(0.04)
local pos2 = input.Position
if pos1 == pos2 and mouseDown then
if ToolEquipped then
mouseDown = true
local startTime = tick()
if not AnimationBusy and Tool.Handle.Transparency < 0.6 then
ANIMATIONS.ThrowCharge:Play(0.1,1,2)
repeat wait() until startTime + THROW_LENGTH <= tick() or not mouseDown
ANIMATIONS.ThrowCharge:Stop()
if not mouseDown and Tool.Handle.Transparency < 0.6 then
stab()
elseif Tool.Handle.Transparency < 0.6 then
if startTime + THROW_DELAY <= tick() and canThrow then
canThrow = false
local ray = Camera:ViewportPointToRay(input.Position.x, input.Position.y,1000)
local ignore = game.Workspace.KnifeHost
if game.Workspace:FindFirstChild("Map") then
ignore = game.Workspace.Map:FindFirstChild("IGNORE")
end
local part, hit = game.Workspace:FindPartOnRayWithIgnoreList(ray, {Player.Character})
game.ReplicatedStorage.Remotes.ThrowKnife:FireServer(hit.x, hit.Y, hit.Z, hit, effectTransparency)
Tool.Handle.Swoosh.Pitch = math.random(1100,1300)/1000
Tool.Handle.Swoosh:Play()
ANIMATIONS.Throw:Play(0.1,1,3)
throwing = true
coroutine.wrap(function()
wait(1.2)
canThrow = true
end)()
wait(1)
Tool.Handle.Transparency = effectTransparency
throwing = false
end
end
end
end
end
end
end)
UserInputService.InputEnded:connect(function(input, gameProcessed)
mouseDown = false
end)
end