#How to make sword keep track of kills

28 messages · Page 1 of 1 (latest)

kindred swan
#

So far, I've only managed to make the gun keep track of kills. But I want to make the grenades and the knife keep track of kills also. I don't know how to do that. BTW here is the knife script:

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Slash = script:WaitForChild("Slash")
local Debounce = false
local PlayersHit = {}
local stabSFX = script.Parent.Handle["Knife Stab 3 (SFX)"]:Clone()
stabSFX.Parent = script.Parent

Tool.Activated:Connect(function()

if Debounce ==false then
    Debounce = true

    local Humanoid = Tool.Parent:WaitForChild("Humanoid")
    local AnimTrack = Humanoid:LoadAnimation(Slash)

    AnimTrack:Play()
    stabSFX:Play()
    wait(0.7) --Cooldown for next Slash
    Debounce = false
end

end)

Handle.Touched:Connect(function(Hit)

if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= Tool.Parent then

    if Debounce == true and PlayersHit[Hit.Parent] == nil then

        Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(80) --This is the amount of health taken away from one hit

        PlayersHit[Hit.Parent] = true
        wait(1)
        PlayersHit[Hit.Parent] = nil
    end
end

end)

#

This is the script that the grenade uses:
https://www.youtube.com/watch?v=9VK9KJpKaMk

I'm going to show you how to make this awesome, fun using grenade

Subscribe for more Roblox Studio content, leave a like if you enjoy it, and leave a comment about what you think about it! Peace out and enjoy!

#Roblox #RobloxStudio #RobloxTutorials

Time Stamps:
0:00 Intro
0:32 Make the Tool
1:11 Make the Animation
2:12 Adding Items to the Too...

▶ Play video
#

And this is the script that the leaderboard uses:
local function onPlayerJoin(player)
local leaderstats = Instance.new('Folder') --creates a new folder
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player

local kills = Instance.new('IntValue') --creates int value
kills.Name = "Kills" --name of the kills
kills.Value = 0 --its value
kills.Parent = leaderstats --parent of kills (same goes for deaths)

local deaths = Instance.new('IntValue')
deaths.Name = "Deaths"
deaths.Value = 0
deaths.Parent = leaderstats

local killer = Instance.new('StringValue') --makes a killer tag for the player
killer.Name = 'Killer'
killer.Parent = player

end

game.Players.PlayerAdded:Connect(onPlayerJoin) --when a player joins the game

--Death Tracker

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character) --character gets added to the player
character:WaitForChild('Humanoid').Died:Connect(function() --waiting for the humanoid object to load in
drop_ammo(character.HumanoidRootPart.Position) --identifies position where humanoid dies for ammo drop
drop_health(character.HumanoidRootPart.Position) --identifies position where humanoid dies for health drop
player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
local killerTag = game.Players:FindFirstChild(player.Killer.Value) --looks for player folder, finds player who died, locate killer tag + find value from it
if killerTag then
killerTag.leaderstats.Kills.Value = killerTag.leaderstats.Kills.Value + 1
end
end)
end)
end)

woven eagle
#

Can show us gun script

#

Maybe we can port over that behavior

kindred swan
#

This is the local script inside the gun

#

This is the script inside ServerStorage
local bullet = script.Parent
local headshot_sound = game.Workspace["Headshot Sound"]

local function player_check(otherPart) --object that bullet comes in contact with
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid and humanoid.Parent.Name ~= bullet.Attacker.Value then
if otherPart.Name == 'Head' then
humanoid:TakeDamage(60) --taking damage and amount of damage humanoid took
--player.PlayerGui.ScreenGui.Headshot.Visible = true
headshot_sound:Play()
wait(2)
--player.PlayerGui.ScreenGui.Headshot.Visible = false
else
humanoid:TakeDamage(40) --taking damage and amount of damage humanoid took
end
local player = game.Players:FindFirstChild(humanoid.Parent.Name) --do extra checks so script doesn't break
if player then
local tag = player:FindFirstChild('Killer')
if tag then
tag.Value = bullet.Attacker.Value
bullet:Destroy()
end
end
end
end

bullet.Touched:Connect(player_check)

#

I tried posting the second part of the local script so you can copy-paste it but got an error saying: "Your message could not be delivered. This is usually because you don't share a server with the recipient or the recipient is only accepting direct messages from friends. You can see the full list of reasons here: "

last jettyBOT
#

studio** You are now Level 2! **studio

kindred swan
#

Also sorry for late reply i was sleeping at that time

kindred swan
# kindred swan This is the script inside ServerStorage local bullet = script.Parent local heads...

This is the script inside ServerScriptService:
local ReplicatedStorage = game:GetService('ReplicatedStorage') --server side
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local ServerStorage = game:GetService('ServerStorage') --defining ServerStorage
print("server side")

remoteEvent.OnServerEvent:Connect(function(player, gunPos, mosPos) --player triggers event, gunposition, mouseposition

local bullet = Instance.new('Part') --creating a part
bullet.Name = "Bullet"
bullet.Parent = game.Workspace
bullet.Shape = Enum.PartType.Cylinder -- bullet is a cylinder
bullet.Size = Vector3.new(0.5, 0.25, 0.5) -- 3d bullet size
bullet.BrickColor = BrickColor.new('Sand yellow metallic')

local damageScript = ServerStorage:FindFirstChild('Damage'):Clone()
damageScript.Parent = bullet
print("using damage script")

local attacker = Instance.new('StringValue')
attacker.Name = 'Attacker' --name of stringvalue
attacker.Parent = bullet
attacker.Value = player.Name
print("attacker")

local distance = (mosPos - gunPos).magnitude --distance between mouseposition and gunposition
local speed = 900
bullet.CFrame = CFrame.new(gunPos, mosPos) --position of bullet ingame
bullet.Velocity = bullet.CFrame.LookVector * speed -- velocity of bullet
bullet.Orientation = Vector3.new(0, -90, 0)
print("function created")

game:GetService('Debris'):AddItem(bullet, 2.5) --garbage cleanup system for performance

end)

#

all of these scripts are for the handgun

kindred swan
#

Uhh anyone

weak holly
#
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Slash = script:WaitForChild("Slash")
local Debounce = false
local PlayersHit = {}
local stabSFX = script.Parent.Handle["Knife Stab 3 (SFX)"]:Clone()
stabSFX.Parent = script.Parent

Tool.Activated:Connect(function() --(1)

    if Debounce == false then
        Debounce = true

        local Humanoid = Tool.Parent:WaitForChild("Humanoid")
        local AnimTrack = Humanoid:LoadAnimation(Slash)

        AnimTrack:Play()
        stabSFX:Play()
        wait(0.7) -- Cooldown for next Slash
        Debounce = false

        local player = game.Players:GetPlayerFromCharacter(Humanoid.Parent) --(2)

        if player then
            local tag = player:FindFirstChild('Killer')
            if tag then
                tag.Value = player.Name
            end
        end

    end
end)

Handle.Touched:Connect(function(Hit)

    if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= Tool.Parent then

        if Debounce == true and PlayersHit[Hit.Parent] == nil then

            Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(80) -- This is the amount of health taken away from one hit

            PlayersHit[Hit.Parent] = true
            wait(1)
            PlayersHit[Hit.Parent] = nil
        end
    end
end)
```*(Check comments)*
`(1)` tells me that this is an event for the attacker, so all you have to do is getting the `Player` of the victim in `(2)` and not take the `Player` of the attacker.
#

Now I look even more, I think it is better to or should tag the victim inside the Handle.Touched event.

#

Wait, ignore the one above, lemme fix that.

kindred swan
weak holly
#
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Slash = script:WaitForChild("Slash")
local Debounce = false
local PlayersHit = {}
local stabSFX = script.Parent.Handle["Knife Stab 3 (SFX)"]:Clone()
stabSFX.Parent = script.Parent

Tool.Activated:Connect(function()

    if Debounce == false then
        Debounce = true

        local Humanoid = Tool.Parent:WaitForChild("Humanoid")
        local AnimTrack = Humanoid:LoadAnimation(Slash)

        AnimTrack:Play()
        stabSFX:Play()
        wait(0.7) -- Cooldown for next Slash
        Debounce = false
    end
end)

Handle.Touched:Connect(function(Hit)

    if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= Tool.Parent then

        if Debounce == true and PlayersHit[Hit.Parent] == nil then
            --(1)
            local tag = game.Players:GetPlayerFromCharacter(Hit.Parent):FindFirstChild('Killer')
            if tag then
                tag.Value = game.Players:GetPlayerFromCharacter(Tool.Parent).Name
            end
            --[1]

            Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(80) -- This is the amount of health taken away from one hit

            PlayersHit[Hit.Parent] = true
            wait(1)
            PlayersHit[Hit.Parent] = nil
        end
    end
end)
```Ok, test this out, I have inserted the code in `1` and you can remove those comments.
#

Also, tbh, make your code more readable so it is easier to understand what the code does, not only it is easier for others to help/understand/review your code but also for yourself in the future. You can stop using comments for self-explanatory lines such as:

:TakeDamage(•••) -- This is the amount of health taken away from one hit
```It does no difference other than it being there. It says in the code itself that it **takes damage.**
#

You do that a lot. :/

#

@kindred swan

kindred swan
# weak holly You do that a lot. :/

I'm a noob at scripting so I just found this code on the internet and put comments to help explain my single disfunctional braincell but alr I'll try make it more readable next time

weak holly
#

I have edited the code, how about now? You just need to replace the 1.

kindred swan
#

It works, ty! Only took like 24 messages

weak holly
#

It is actually quite easy to figure out, but it did take time to understand the whole code. I recommend basic understanding of Roblox API and Luau.

#

Ok bye. 👋