Excuse me for being not the greatest coder. I have tried implementing where when you kill someone with your melee, you get +1 kill added to your leaderstat. I have tried implementing it myself before, didn’t work. I then tried following some tutorial, still didn’t work. If anyone knows how I can implement it, please let me know.
Script inside the Blade part (basically the hitbox):
local cooldown = false
script.Parent.Touched:Connect(function(hit)
local humanoid=nil
if hit.Parent:FindFirstChild("Humanoid") then humanoid=hit.Parent:FindFirstChild("Humanoid") end
if humanoid==nil then
if hit.Parent.Parent:FindFirstChild("Humanoid") then humanoid=hit.Parent.Parent:FindFirstChild("Humanoid") end
end
if humanoid ~=nil then
if script.Parent.Parent.CanDamage.Value == true then
if cooldown == false then
cooldown = true
humanoid.Health -= 25
script.Parent.Parent.Axe.HitSound:Play()
wait(1)
cooldown = false
end
end
end
end)
I’ve made something similar to this, you would have to tagHumanoid(hum,player) and then untagHumanoid(humanoid). This means that you would have to set the owner tag to the player who fired the gun.
local players = game:GetService("Players")
function tagHumanoid(hum,player)
local ownerTag = Instance.new("ObjectValue")
ownerTag.Name = "owner"
ownerTag.Value = player
game.Debris:AddItem(ownerTag, 5)
ownerTag.Parent = hum
end
function untagHumanoid(humanoid)
for i,v in pairs (humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name = "owner" then
v:Destroy()
end
end
end
local function playerData(player)
local leaderstats = Instance.new("Folder", player)
local kills = Instance.new("IntValue",leaderstats)
kills.Name = "Kills"
end
players.PlayerAdded:Connect(function(player)
playerData(player)
local character = player.Character or player.Character:Wait()
local hum = character:FindFirstChild("Humanoid")
hum.Died:Connect(function()
for i,v in pairs(hum:GetChildren()) do
if v.Name == "owner" then
local newPlrVals = v.Value
newPlrVals.leaderstats.Kills.Value += 1
end
end
end)
end)
Inside the touched event, you can create a seperate function for the playerAdded event so that you access the player. When a player dies, it should find the other player that used a weapon.
Here is my better version of your script and leaderstats
Script Inside ServerScriptService:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = leaderstats
local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats
plr.CharacterAdded:Connect(function(character)
if character then
local LastHited = Instance.new("IntValue")
LastHited.Name = "LastHited"
LastHited.Parent = character
end
character:FindFirstChild("Humanoid").Died:Connect(function()
Deaths.Value += 1
local LastHited:IntValue = character:FindFirstChild("LastHited")
local UserId = LastHited.Value
if UserId then
local player = Players:GetPlayerByUserId(UserId)
if player then
player.leaderstats.Kills.Value += 1
end
end
end)
end)
end)
Your Script for blade:
I added to hit multiple player at once but keep in mind i removed cooldown because it will effect hiting multiple players,also ,sword has not global cooldown system hiting different players cooldown is different if you want for me to create for you better weapon system just ask:
Edit : I fixed script
local Players = game:GetService("Players")
local Tool = script.Parent.Parent
local Axe = Tool:FindFirstChild("Axe")
local Blade = Tool:FindFirstChild("Blade")
local CanDamage = Tool:FindFirstChild("CanDamage")
local HitSound = Axe:FindFirstChild("HitSound")
local Cooldown = false
local HitedChars = {}
Blade.Touched:Connect(function(hit:BasePart)
local Char = hit.Parent
local Humanoid = Char:FindFirstChildOfClass("Humanoid")
local LastHited:IntValue = Char:FindFirstChild("LastHited")
if Humanoid and Char and Char ~= Tool.Parent and LastHited then
local Player = Players:GetPlayerFromCharacter(Char)
if CanDamage.Value then
if not HitedChars[Char] then
LastHited.Value = Player.UserId
HitedChars[Char] = true
Humanoid:TakeDamage(25)
HitSound:Play()
task.wait(1)
HitedChars[Char] = false
end
end
end
end)
Oh sorry i made mistake on this line in blade script
LastHited.Value = Char.Name
Fully Fixed Script:
local Players = game:GetService("Players")
local Tool = script.Parent.Parent
local Axe = Tool:FindFirstChild("Axe")
local Blade = Tool:FindFirstChild("Blade")
local CanDamage = Tool:FindFirstChild("CanDamage")
local HitSound = Axe:FindFirstChild("HitSound")
local Cooldown = false
local HitedChars = {}
Blade.Touched:Connect(function(hit:BasePart)
local Char = hit.Parent
local Humanoid = Char:FindFirstChildOfClass("Humanoid")
local LastHited:IntValue = Char:FindFirstChild("LastHited")
if Humanoid and Char and Char ~= Tool.Parent and LastHited then
local Player = Players:GetPlayerFromCharacter(Char)
if CanDamage.Value then
if not HitedChars[Char] then
LastHited.Value = Player.UserId
HitedChars[Char] = true
Humanoid:TakeDamage(25)
HitSound:Play()
task.wait(1)
HitedChars[Char] = false
end
end
end
end)
Assuming that the script.Parent is the blade, you could possibly add a tag/attribute to the blade for the owner of the blade. Then, in the part where it deducts the Humanoid’s health, add a
if Humanoid.Health <= 0 then
-- get the "owner" of the blade and add 1 kill to that player
end
If you don’t know how to add the “owner”, assuming you have an RemoteEvent to swing the blade:
edit: also, you should probably add the cooldown on the swinging of the blade, not when the blade hits, because right now as long as the player doesn’t hit any other player the player can swing the blade w/o cooldown (i’m not sure if this is a feature in your game but just to point out in case it isn’t)
if UserId then
local player = Players:GetPlayerByUserId(UserId)
if player then
player.leaderstats.Kills.Value += 1
end
end
plr is player who died but player is a player who recives kill stats maybe you though that is same player and doesn’t need to have two variables but wrong it should be two players variable.
That is problem i tested leaderstats script and it doesn’t have problem.
Possibility 2: If you use Local Test it would not always work because they use same UserId but my script uses UserId to find player by userId you can test on roblox client and it will work then.
Can you show screenshoot or videos or error messages i can’t quite understand what do you mean by crediting to different player?
This should write value for LastHited by player who touches Blade part
LastHited.Value = Player.UserId
This part find last hitted value and credits player who last hited before killing:
local LastHited:IntValue = character:FindFirstChild("LastHited")
local UserId = LastHited.Value
if UserId then
local player = Players:GetPlayerByUserId(UserId)
if player then
player.leaderstats.Kills.Value += 1
end
end
At the moment I can’t get any screenshots or anything. However, what I mean is that the player you kill will get +1 death and also +1 kill. The dead player gets the kill instead of the player that killed them.
Is this your full script inside of blade i think problem is this script that overwrites character if your character touches blade ,maybe its that problem:
Try this and let me know if this is a still problem if its problem is very unlikely to be leaderstats because i tested and it works fine even in Local Test.
I can script your new axe system tool if its still doesnt work i can script you new system?:
I added to check if blade is descendant of that char if is not then will damage
local Players = game:GetService("Players")
local Tool = script.Parent.Parent
local Axe = Tool:FindFirstChild("Axe")
local Blade = Tool:FindFirstChild("Blade")
local CanDamage = Tool:FindFirstChild("CanDamage")
local HitSound = Axe:FindFirstChild("HitSound")
local Cooldown = false
local HitedChars = {}
Blade.Touched:Connect(function(hit:BasePart)
local Char = hit.Parent
local Humanoid = Char:FindFirstChildOfClass("Humanoid")
local LastHited:IntValue = Char:FindFirstChild("LastHited")
if Humanoid and Char and Char ~= Tool.Parent and not Blade:IsDescendantOf(Char) and LastHited then
local Player = Players:GetPlayerFromCharacter(Char)
if CanDamage.Value then
if not HitedChars[Char] then
LastHited.Value = Player.UserId
HitedChars[Char] = true
Humanoid:TakeDamage(25)
HitSound:Play()
task.wait(1)
HitedChars[Char] = false
end
end
end
end)
Here is my sword system i made long time ago:
I tested with that as weapon maybe you can create simular system like that:
Setup:
Local Script:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
repeat wait() until player.Character
local MCharacter = player.Character
local MainHumanoid = MCharacter.Humanoid
local Tool = script.Parent
local Cooldown = false
local Animation = Tool.SwingAnimation
Tool.Activated:Connect(function()
if not Cooldown then
Cooldown = true
local LoadedAnimation = MainHumanoid:LoadAnimation(Animation)
LoadedAnimation.Priority = Enum.AnimationPriority.Idle -- set the priority to Idle
LoadedAnimation:Play()
Tool.SwingEvent:FireServer()
task.wait(1)
LoadedAnimation:Stop()
Cooldown = false
end
end)
ServerScript:
local Debris = game:GetService("Debris")
local Tool = script.Parent
local SwingEvent = Tool.SwingEvent
local handle = Tool.Handle
SwingEvent.OnServerEvent:Connect(function(player)
local debounce = true
local Hitbox = Instance.new("Part")
Hitbox.Parent = handle
Hitbox.Size = Vector3.new(5,5,5)
Hitbox.Transparency = 1
Hitbox.Massless = true
Hitbox.CFrame = handle.CFrame
Hitbox.CanCollide = false
local weld = Instance.new("WeldConstraint")
weld.Part0 = handle
weld.Part1 = Hitbox
weld.Parent = handle
Debris:AddItem(Hitbox,1)
Debris:AddItem(weld,1)
local HitedChars = {}
Hitbox.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
local char = hit.Parent
if char and hum and char ~= player.Character then
if not HitedChars[char] then
HitedChars[char] = true
char.LastHited.Value = player.UserId
hum:TakeDamage(15)
end
end
end)
end)