Whenever the attacker shoots the gun, it hits the player. I made a humanoid.died event so that when the person getting shot at dies from the gun, the leaderstats go up. This isn’t the case for some reason. I’m getting no errors from the output so I’m unsure of what’s happening. Any suggestions?
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
remote.OnServerEvent:Connect(function(player, position, shootpartPos)
local raycastpa = RaycastParams.new()
raycastpa.FilterDescendantsInstances = {player.Character}
raycastpa.FilterType = Enum.RaycastFilterType.Blacklist
local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)
if raycastresult then
local hitpart = raycastresult.Instance
local model = hitpart:FindFirstAncestorOfClass("Model")
if model then
if model:FindFirstChild("Humanoid") then
model.Humanoid.Health -= 5
print(position)
end
end
if model:FindFirstChild("Humanoid").Died == true then
local ldrstats = player.leaderstats
ldrstats.Kills.Value = ldrstats.Kills.Value + 1
end
end
end)
First off, formatting your code properly may help, performance-wise and also emotionally.
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
remote.OnServerEvent:Connect(function(player, position, shootpartPos)
local raycastpa = RaycastParams.new()
raycastpa.FilterDescendantsInstances = {player.Character}
raycastpa.FilterType = Enum.RaycastFilterType.Blacklist
local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)
if raycastresult then
local hitpart = raycastresult.Instance
local model = hitpart:FindFirstAncestorOfClass("Model")
if model then
humanoid = model:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health -= 5
print(position)
if humanoid.Health <= 0 then
player.leaderstats.Kills.Value += 1
end
end
end
end
end)
This partially works. When the player dies, it does log the kill on the stats; however, I can keep shooting at the body and the kills will still go up.
-- damage script
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
remote.OnServerEvent:Connect(function(player, position, shootpartPos)
local raycastpa = RaycastParams.new()
raycastpa.FilterDescendantsInstances = {player.Character}
raycastpa.FilterType = Enum.RaycastFilterType.Blacklist
local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)
if raycastresult then
local hitpart = raycastresult.Instance
local model = hitpart:FindFirstAncestorOfClass("Model")
if model then
humanoid = model:FindFirstChild("Humanoid")
if humanoid then
if humanoid.Health > 0 and not humanoid:FindFirstChild("deathreal") then
local Obj = Instance.new("ObjectValue")
Obj.Name = "deathreal"
Obj.Value = player
Obj.Parent = humanoid
humanoid.Health -= 5
print(position)
end
end
end
end
end)
--- Main script
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
Humanoid.Died:Connect(function()
if Humanoid:FindFirstChildOfClass("ObjectValue") then
local Killer = Humanoid:FindFirstChild("deathreal")
local Player = Killer.Value
Player.leaderstats.Kills.Value += 1
task.wait()
Killer:Destroy()
end
end)
end)
end)
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
function GiveOBJ(Him: Humanoid?, He:Player?)
if not Him:FindFirstChild("deathreal") then
local Obj = Instance.new("ObjectValue")
Obj.Name = "deathreal"
Obj.Value = He
Obj.Parent = humanoid
game:GetService("Debris"):AddItem(Obj, 10)
else
Him:FindFirstChild("deathreal"):Destroy()
local Obj = Instance.new("ObjectValue")
Obj.Name = "deathreal"
Obj.Value = He
Obj.Parent = humanoid
game:GetService("Debris"):AddItem(Obj, 10)
end
end
remote.OnServerEvent:Connect(function(player, position, shootpartPos)
local raycastpa = RaycastParams.new()
raycastpa.FilterDescendantsInstances = {player.Character}
raycastpa.FilterType = Enum.RaycastFilterType.Blacklist
local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)
if raycastresult then
local hitpart = raycastresult.Instance
local model = hitpart:FindFirstAncestorOfClass("Model")
if model then
humanoid = model:FindFirstChild("Humanoid")
if humanoid then
if humanoid.Health > 0 then
GiveOBJ(humanoid, player)
humanoid.Health -= 5
print(position)
end
end
end
end
end)
This will work. What you’re trying to do will not work. This SHOULD work:
if model:FindFirstChild("Humanoid") then
model.Humanoid.Died:Connect(function()
local ldrstats = player.leaderstats
ldrstats.Kills.Value = ldrstats.Kills.Value + 1
end)
end
I added your snippet to the top, it didn’t work. It deals damage, but doesnt add onto the leaderstat. I’m getting no errors in the output.
-- damage script
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
local tool = script.Parent
local remote = tool:WaitForChild("OnShot")
function GiveOBJ(Him: Humanoid?, He:Player?)
if not Him:FindFirstChild("deathreal") then
local Obj = Instance.new("ObjectValue")
Obj.Name = "deathreal"
Obj.Value = He
Obj.Parent = humanoid
game:GetService("Debris"):AddItem(Obj, 10)
else
Him:FindFirstChild("deathreal"):Destroy()
local Obj = Instance.new("ObjectValue")
Obj.Name = "deathreal"
Obj.Value = He
Obj.Parent = humanoid
game:GetService("Debris"):AddItem(Obj, 10)
end
end
remote.OnServerEvent:Connect(function(player, position, shootpartPos)
local raycastpa = RaycastParams.new()
raycastpa.FilterDescendantsInstances = {player.Character}
raycastpa.FilterType = Enum.RaycastFilterType.Blacklist
local raycastresult = workspace:Raycast(shootpartPos, (position - shootpartPos).Unit*3000, raycastpa)
if raycastresult then
local hitpart = raycastresult.Instance
local model = hitpart:FindFirstAncestorOfClass("Model")
if model then
humanoid = model:FindFirstChild("Humanoid")
if humanoid then
if humanoid.Health > 0 then
GiveOBJ(humanoid, player)
humanoid.Health -= 5
print(position)
end
end
end
end
end)
--- Main script
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
Humanoid.Died:Connect(function()
if Humanoid:FindFirstChildOfClass("ObjectValue") then
local Killer = Humanoid:FindFirstChild("deathreal")
local Player = Killer.Value
Player.leaderstats.Kills.Value += 1
task.wait()
Killer:Destroy()
end
end)
end)
end)
Oops, I made a minor mistake…
Replace GiveOBJ function with this.
function GiveOBJ(Him: Humanoid?, He:Player?)
if not Him:FindFirstChild("deathreal") then
local Obj = Instance.new("ObjectValue")
Obj.Name = "deathreal"
Obj.Value = He
Obj.Parent = Him
game:GetService("Debris"):AddItem(Obj, 10)
else
Him:FindFirstChild("deathreal"):Destroy()
local Obj = Instance.new("ObjectValue")
Obj.Name = "deathreal"
Obj.Value = He
Obj.Parent = Him
game:GetService("Debris"):AddItem(Obj, 10)
end
end