Hi, I designed a knife for my game, but when the knife touches the person, it does 99% damage instead of killing them, which means I have to kill that person over and over again. Now I will give you the commands and let me know if there is anything I need to fix.
- Knife
damage = 100
swingrate = 0.9
anims={“GenericStab1”,“GenericStab2”,“StabPunch”,“Backstab”}
lastAnim = “”
bloodeffects = true
ready = false
equipped = false
lastswing = 0
local storage = script.Parent:WaitForChild(“Storage”)
local down = storage:WaitForChild(“MouseDown”)
local debris = game:GetService(“Debris”)
local state = script.Parent:WaitForChild(“SetState”)
local throw = script.Parent:WaitForChild(“RequestThrow”)
local animEvent = script.Parent:WaitForChild(“PlayAnimation”)
local soundEvent = script.Parent:WaitForChild(“PlayClientSound”)
function WaitFor(start,…)
local obj = start
for _,v in pairs{…} do
obj = obj:WaitForChild(v)
end
return obj
end
local player
while not player do
if script.Parent.Parent:IsA(“Model”) then
player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
elseif script.Parent.Parent:IsA(“Backpack”) then
player = script.Parent.Parent.Parent
end
end
function runsound(id,volume)
local volume = volume or 1
local sound = Instance.new(“Sound”)
sound.Looped = false
sound.Pitch = 1
sound.SoundId = “http://www.roblox.com/asset/?id=”…tostring(id)
sound.PlayOnRemove = false
sound.Volume = volume
debris:AddItem(sound,3)
sound.Parent = script.Parent.Handle
wait()
sound:Play()
end
function runAnimation(anim)
if anim == “Backstab” then
Spawn(function ()
script.Parent.GripUp = Vector3.new(0,-1,0)
wait(.55)
script.Parent.GripUp = Vector3.new(0,1,0)
end)
end
animEvent:FireAllClients(anim)
end
function clientSound(ref)
soundEvent:FireAllClients(ref)
end
b = Instance.new(“Part”)
b.BrickColor = BrickColor.new(“Bright red”)
b.formFactor = “Custom”
b.CanCollide = false
b.TopSurface = “Smooth”
b.BottomSurface = “Smooth”
function makeblood(part)
local bin = script.Parent.Parent:findFirstChild(“BloodBin”) or Instance.new(“Model”,script.Parent.Parent)
bin.Name = “BloodBin”
local function r(a,b)
– This is basically math.random with floats instead of integers.
a = a or 0; b = b or 1
return a+(b-a)math.random()
end
if part then
local bl = b:clone()
bl.Transparency = r().5
bl.Size = Vector3.new(.2r(1,5),.2,.2r(1,5))
bl.Velocity = part.Velocity+(Vector3.new((r()-.5),(r()-.5),(r()-.5))30)
bl.RotVelocity = part.RotVelocity+(Vector3.new((r()-.5),(r()-.5),(r()-.5))20)
bl.CFrame = part.CFrameCFrame.new((r()-.5)3,(r()-.5)3,(r()-.5)3)CFrame.Angles(math.pi2r(),math.pi2r(),math.pi2*r())
bl.Parent = workspace
end
end
didHit = false
swinging = false
function ThrowAttack(_,target)
script.Parent.Enabled = false
script.Parent.Handle.Throw:Play()
wait(0.2)
local Handle = script.Parent.Handle
local Humanoid = script.Parent.Parent:findFirstChild(“Humanoid”)
local touchedConn
local throwingHandle
if Humanoid then
Handle.Archivable = true
throwingHandle = Handle:Clone()
throwingHandle.Name = “ThrownKnife”
throwingHandle.Transparency = 0
script.ThrowSmoke:clone().Parent = throwingHandle
game:GetService(“Debris”):AddItem(throwingHandle, 5)
throwingHandle.Parent = Humanoid.Parent
throwingHandle.Velocity = (target - throwingHandle.CFrame.p).unit * 200
throwingHandle.CFrame = CFrame.new(throwingHandle.CFrame.p, throwingHandle.CFrame.p + throwingHandle.Velocity) * CFrame.Angles(0, 0, math.rad(-90))
local floatingForce = Instance.new(‘BodyForce’, throwingHandle)
floatingForce.force = Vector3.new(0, 196.2 * throwingHandle:GetMass(), 0)
local spin = Instance.new(‘BodyAngularVelocity’, throwingHandle)
spin.angularvelocity = throwingHandle.CFrame:vectorToWorldSpace(Vector3.new(0, -400, 0))
end
wait(0.08)
if throwingHandle then
throwingHandle.CanCollide = true
Handle.Transparency = 1
touchedConn = throwingHandle.Touched:connect(function (hit)
if hit.Transparency == 1 then return end
if hit.Name == “Handle” then return end
if hit.Parent == Humanoid.Parent then return end
if hit.Parent:findFirstChild(“Humanoid”) == nil then return end
if throwingHandle.Transparency ~= 0 then return end
hit.Parent.Humanoid:TakeDamage(damage)
delay(3,function ()
(throwing) = false
script.Parent.Enabled = true
Handle.Transparency = 0
end)
clientSound(“Critical”)
throwingHandle.Anchored = true
for i = 1,10 do
throwingHandle.Transparency = i*.1
wait()
end
throwingHandle:Destroy()
touchedConn:disconnect()
end)
end
local proceed = true
for i = 1,80 do
if script.Parent.Enabled then
proceed = false
elseif proceed then
wait()
end
end
Handle.Transparency = 0
script.Parent.Enabled = true
throwing = false
end
function closestPlayerInSight(characters)
local torso = script.Parent.Parent:findFirstChild(“Torso”)
if torso then
local closestTorso,closestDist = nil,math.huge
for _,v in pairs(characters) do
local dist = (v.Torso.Position-torso.Position).magnitude
if dist <= 8 and dist < closestDist then
closestTorso = v.Torso
closestDist = dist
end
end
if closestTorso and closestDist then
return closestTorso.Parent
end
end
end
function Activate()
if equipped and (tick()-lastswing)>=swingrate and ready and script.Parent.Enabled and script.Parent.Handle.Transparency == 0 and script.Parent.Parent:findFirstChild(“Head”) then
script.Parent.Enabled = false
didHit = false
newanim = anims[math.random(1,#anims)]
runAnimation(newanim)
lastAnim = newanim
lastswing = tick()
swinging = true
local characters = {}
for _,v in pairs(game.Players:GetPlayers()) do
if v ~= player then
if v.Character and v.Character:findFirstChild(“Torso”) and v.Character:findFirstChild(“Humanoid”) and v.Character.Humanoid.Health > 0 then
table.insert(characters,v.Character)
end
end
end
for _,v in pairs(workspace:GetChildren()) do
if v:IsA(“Model”) and v:findFirstChild(“Humanoid”) and v.Humanoid.Health > 0 and v ~= script.Parent.Parent then
table.insert(characters,v)
end
end
for i = 1,6 do
if not didHit then
local userToKill = closestPlayerInSight(characters)
if userToKill then
userToKill.Humanoid:TakeDamage(damage)
didHit = true
clientSound(lastAnim == “Backstab” and “Critical” or “Normal”)
local numbloodeffects = lastAnim == “Backstab” and math.random(100,150) or math.random(20,40)
for i = 1,math.random(numbloodeffects-1,numbloodeffects+1) do
makeblood(userToKill.Torso)
end
end
end
wait(0.05)
end
if not didHit then – If we didn’t hit a player immediately, then play a swing sound to the server
script.Parent.Handle.Swing:Play()
end
swinging = false
didHit = false
wait(0.6)
script.Parent.Enabled = true
if down.Value then
Activate()
end
end
end
down.Changed:connect(function()
if down.Value then
Activate()
end
end)
hu = nil
script.Parent.Equipped:connect(function ()
ready = true
equipped = true
script.Parent.Handle.Transparency = 0
hu = script.Parent.Parent:findFirstChild(“Humanoid”)
if hu then
hu.WalkSpeed = 20
end
if down.Value then
Activate()
end
end)
state.OnServerEvent:connect(function (player,b)
down.Value = b
end)
throw.OnServerEvent:connect(ThrowAttack)
script.Parent.Unequipped:connect(function ()
down.Value = false
if hu then
hu.WalkSpeed = 20
hu = nil
end
ready = false
equipped = false
end)
- Knife Script
local parent = script.Parent
local Handle = parent:WaitForChild(“Handle”)
local Creator = parent:WaitForChild(“Creator”)
CanDmg = false
Can = true
parent.Equipped:Connect(function()
Handle.Equip:Play()
if Creator.Value == nil then
Creator.Value = parent.Parent end
end)
parent.Unequipped:Connect(function()
Handle.UnEquip:Play()
end)
repeat wait() until Creator.Value ~= nil
parent.Activated:Connect(function()
if Handle.Transparency == 1 then return end
function slash()
print(“hip”)
local creator = Creator.Value
local Animations = parent:WaitForChild(“Animations”)
local AnimChild = Animations:GetChildren()
local AnimRad = math.random(1,#AnimChild)
local AnimSel = AnimChild[AnimRad]
local AP = creator:WaitForChild(“Humanoid”):LoadAnimation(AnimSel)
if Can == true then
Can = false
AP:Play()
wait(0.15)
CanDmg = true
Handle.Swing:Play()
Handle.SlashTrail.Enabled = true
wait(0.25)
CanDmg = false
Handle.SlashTrail.Enabled = false
wait(0.06)
Can = true
end
end
slash()
end)
function on(t)
local h = t.Parent:FindFirstChildOfClass(“Humanoid”)
if h ~= nil and CanDmg == true then
CanDmg = false
local cre = Creator.Value
h:TakeDamage(parent.Dmg.Value)
Handle.Swing:Stop()
Handle.Hit:Play()
if h.Health>0 then
if not h:FindFirstChild(“creator”) then
local ov = Instance.new(“ObjectValue”,h)
ov.Name = “creator”
ov.Value = game.Players:WaitForChild(cre.Name)
else
local ovs = h:GetChildren()
for i = 1,#ovs do
if (ovs[i].Name == “creator”) then
ovs[i].Value = game.Players:WaitForChild(cre.Name) end
end
end
end
end
end
Handle.Touched:Connect(on)
- Local Knife
local storage = script.Parent:WaitForChild(“Storage”)
local down = storage:WaitForChild(“MouseDown”)
local runanim = storage:WaitForChild(“RunAnim”)
local cs = script.Parent:WaitForChild(“PlayClientSound”)
local state = script.Parent:WaitForChild(“SetState”)
local throw = script.Parent:WaitForChild(“RequestThrow”)
local animEvent = script.Parent:WaitForChild(“PlayAnimation”)
animspeed = 1
equipspeed = 1.5
throwing = false
function WaitForChildren(start,…)
if not start:IsA(“Instance”) then return end
local currentObj = start
for _,v in pairs{…} do
if type(v) == “string” then
currentObj = currentObj:WaitForChild(v)
end
end
return currentObj
end
function update(mouse)
if mouse ~= nil then
mouse.Icon = script.Parent.Enabled and “rbxasset://textures\GunCursor.png” or “rbxasset://textures\GunWaitCursor.png”
end
end
heatStorage = {}
local hitSound
local input = game:GetService(“UserInputService”)
local isMobile = input.TouchEnabled
function ClientThrow(target)
– I’m doing this to remove the lag factor of the knife from the client’s perspective
– This knife doesn’t actually do anything to the players, its basically simulating what the server is doing
– Worse case scenario: Accuracy and latency screw it up
local Handle = script.Parent.Handle
local serverKnife = script.Parent.Parent:WaitForChild(“ThrownKnife”)
local clientKnife = Handle:Clone()
clientKnife.Transparency = 0
clientKnife.CanCollide = false
game:GetService(“Debris”):AddItem(clientKnife, 5)
clientKnife.Parent = workspace.CurrentCamera
clientKnife.Velocity = (target - clientKnife.CFrame.p).unit * 200
clientKnife.CFrame = serverKnife.CFrame
local floatingForce = Instance.new(‘BodyForce’, clientKnife)
floatingForce.force = Vector3.new(0, 196.2 * clientKnife:GetMass(), 0)
local spin = Instance.new(‘BodyAngularVelocity’, clientKnife)
spin.angularvelocity = clientKnife.CFrame:vectorToWorldSpace(Vector3.new(0, -400, 0))
serverKnife.Changed:connect(function (property)
if property == “Anchored” then
clientKnife.Anchored = true
clientKnife.CFrame = serverKnife.CFrame
for i = 1,10 do
clientKnife.Transparency = i*.1
wait()
end
clientKnife:Destroy()
end
end)
end
function runAnimation(an)
local h = game.Players.LocalPlayer.Character:findFirstChild(“Humanoid”)
local anim = storage:findFirstChild(an)
if anim and h then
local theanim = h:LoadAnimation(anim)
if theanim and h.Health > -100 then
theanim:Play(nil,nil,animspeed)
if anim.Name == “Backstab” then
script.Parent.GripUp = Vector3.new(0,-1,0)
wait(.75)
script.Parent.GripUp = Vector3.new(0,1,0)
end
delay(1,function ()
theanim:Stop()
end)
end
end
end
animEvent.OnClientEvent:connect(runAnimation)
throwing = false
function setKnifeState(b)
b = throwing and false or b
state:FireServer(b)
end
function requestThrow()
if script.Parent.Handle.Transparency == 0 and not throwing and script.Parent.Enabled then
throwing = true
runAnimation(“Throw”)
local hit = game.Players.LocalPlayer.Character.Humanoid.TargetPoint
throw:FireServer(hit)
ClientThrow(hit)
wait(3)
throwing = false
end
end
function Equipped(mouse)
hitSound = game.Players.LocalPlayer.PlayerGui:findFirstChild(“Sound”) or Instance.new(“Sound”)
hitSound.SoundId = “http://www.roblox.com/asset/?ID=186311262”
hitSound.Parent = game.Players.LocalPlayer.PlayerGui
for _,v in pairs(game.Players:GetPlayers()) do
if v.Character and v.Character.Humanoid.Health > 0 and v.Character:findFirstChild(“Torso”) and v.Character ~= game.Players.LocalPlayer.Character then
local heat = storage.Heat:clone()
heat.Parent = game.Players.LocalPlayer.PlayerGui
heat.Adornee = v.Character.Torso
heat.Enabled = true
table.insert(heatStorage,heat)
v.Character.Humanoid.Died:connect(function ()
heat:Destroy()
end)
heat.Adornee.Changed:connect(function ()
if not heat.Adornee or heat.Adornee.Parent == nil then
heat:Destroy()
end
end)
heat.Changed:connect(function ()
if not heat.Adornee then
heat:Destroy()
end
local scale = math.sqrt(heat.AbsoluteSize.X^2+heat.AbsoluteSize.Y^2)
heat.ImageLabel.Visible = scale > 30
end)
end
end
local input = game:GetService(“UserInputService”)
mouse.Button1Down:connect(function ()
setKnifeState(true)
end)
mouse.Button1Up:connect(function ()
setKnifeState(false)
end)
mouse.Button2Down:connect(function ()
requestThrow()
end)
input.TouchTap:connect(function ()
setKnifeState(true)
wait(.2)
setKnifeState(false)
end)
input.TouchLongPress:connect(function ()
requestThrow()
end)
mouse.Icon = script.Parent.Enabled and “rbxasset://textures/GunCursor.png” or “rbxasset://textures/GunWaitCursor.png”
script.Parent.Changed:connect(function ()
mouse.Icon = script.Parent.Enabled and “rbxasset://textures/GunCursor.png” or “rbxasset://textures/GunWaitCursor.png”
end)
end
function Unequipped()
for _,v in pairs(heatStorage) do
v:Destroy()
end
heatStorage = {}
if hitSound then
hitSound:Destroy()
end
end
script.Parent.Equipped:connect(Equipped)
script.Parent.Unequipped:connect(Unequipped)
cs.OnClientEvent:connect(function (value)
if hitSound then
hitSound.SoundId = (value == “Normal”) and “http://www.roblox.com/asset/?ID=186311262” or “http://www.roblox.com/asset/?ID=186311262”
hitSound.Pitch = 1,0 +(math.random()/1)
hitSound:Play()
end
end)