So, in my Roblox sword I have a modified SwordScript that is modified for my leaderstats stuff such as giving the killed player’s stat to the killer. The issue I’m experiencing and find difficult to fix is how sometimes when you’re fighting with another player with the sword then either one player gets killed or both, of course if it’s one player it’s fairly killed. The script should only and always make only one player get killed, not both.
SwordScript
--[[-----------------------------------------------------------------------------------
>> VARIABLES
-------------------------------------------------------------------------------------]]
local Players = game:FindService("Players")
local Run = game:FindService("RunService")
local Debris = game:FindService("Debris")
local Tool = script.Parent
local Sword = Tool:WaitForChild("Handle")
local Slash_Sound = Sword:WaitForChild("Slash_Sound")
local Lunge_Sound = Sword:WaitForChild("Lunge_Sound")
local Unsheath_Sound = Sword:WaitForChild("Unsheath_Sound")
local Stats = Sword:WaitForChild("Stats")
local Idle_Damage = Stats.Idle.Value
local Slash_Damage = Stats.Slash.Value
local Lunge_Damage = Stats.Lunge.Value
local damage = Idle_Damage
--[[-----------------------------------------------------------------------------------
>> TAG HUMANOID FUNCTION
-------------------------------------------------------------------------------------]]
local function TagHumanoid(S_Player, Other)
local Tag = Instance.new("ObjectValue")
Tag.Value = S_Player
Tag.Name = "creator"
Tag.Parent = Other
Debris:AddItem(Tag, 0.25)
Other:TakeDamage(damage)
end
--[[-----------------------------------------------------------------------------------
>> DAMAGE FUNCTION
-------------------------------------------------------------------------------------]]
local humanoid_connection = nil
local death_effect_manager = require(script.deathEffect)
function on_touch_death_trigger(first_player: Player, back_player: Player)
local humanoid_connection = nil
local leader_stats_first = first_player:FindFirstChild("leaderstats")
local leader_stats_back = back_player:FindFirstChild("leaderstats")
if leader_stats_first and leader_stats_back then
local kills_stat = leader_stats_first:FindFirstChild("Kills")
local time_stat_first = leader_stats_first:FindFirstChild("Time")
local time_stat_back = leader_stats_back:FindFirstChild("Time")
local streak_stat_first = leader_stats_first:FindFirstChild("KillStreak")
if kills_stat and time_stat_back and time_stat_first and streak_stat_first then
if not back_player.Character:GetAttribute("tagged_death") then
back_player.Character:SetAttribute("tagged_death", true)
humanoid_connection = back_player.Character.Humanoid.Died:Connect(function()
kills_stat.Value = kills_stat.Value + 1
time_stat_first.Value = time_stat_first.Value + time_stat_back.Value
time_stat_back.Value = 0
first_player.Character.Humanoid.Health = 100
--death_effect_manager:dead_effect(back_player.Character)
streak_stat_first.Value = streak_stat_first.Value + 1
back_player.Character:SetAttribute("tagged_death", nil)
humanoid_connection:Disconnect()
end)
end
end
end
end
local function onTouched(Hit)
local Other = Hit.Parent:FindFirstChild("Humanoid")
if Other then
local Arm = Tool.Parent:FindFirstChild("Right Arm")
if Arm then
local Grip = Arm:FindFirstChild("RightGrip")
if (Grip and (Grip.Part0 == Arm or Grip.Part1 == Sword)) then
local O_Player = Players:GetPlayerFromCharacter(Hit.Parent)
if O_Player then
local S_Player = Players:GetPlayerFromCharacter(Tool.Parent)
if O_Player.Character:GetAttribute("safe_zone") or S_Player.Character:GetAttribute("safe_zone") then
return
end
on_touch_death_trigger(S_Player, O_Player)
S_Player.Character:SetAttribute("in_combat",true)
O_Player.Character:SetAttribute("in_combat",true)
if S_Player then
TagHumanoid(S_Player, Other)
end
else
Other:TakeDamage(damage)
end
end
end
end
end
--[[-----------------------------------------------------------------------------------
>> SLASH FUNCTION
-------------------------------------------------------------------------------------]]
local function Slash()
damage = Slash_Damage
Slash_Sound:Play()
local Animation = Instance.new("StringValue")
Animation.Name = "toolanim"
Animation.Value = "Slash"
Animation.Parent = Tool
end
--[[-----------------------------------------------------------------------------------
>> LUNGE FUNCTION
-------------------------------------------------------------------------------------]]
local function Lunge()
damage = Lunge_Damage
Lunge_Sound:Play()
local Animation = Instance.new("StringValue")
Animation.Name = "toolanim"
Animation.Value = "Lunge"
Animation.Parent = Tool
wait(0.25)
Tool.GripUp = Vector3.new(1, 0, 0)
wait(0.75)
Tool.GripUp = Vector3.new(0, 0, 1)
end
--[[-----------------------------------------------------------------------------------
>> UNSHEATH FUNCTION
-------------------------------------------------------------------------------------]]
local function onEquipped()
Unsheath_Sound:Play()
local Humanoid = Tool.Parent.Humanoid
Humanoid.Died:Connect(function()
Debris:AddItem(Tool, 0.75)
end)
local player = Players:GetPlayerFromCharacter(Tool.Parent)
if player then
if player.Character then
if player.Character:FindFirstChild("Humanoid") then
player.Character.Humanoid.Died:Connect(function()
local l = player:FindFirstChild("leaderstats")
if l then
local s = l:FindFirstChild("KillStreak")
if s then
if player.Character:GetAttribute("in_combat") or not player.Character:GetAttribute("safe_zone") then
s.Value = 0
end
end
end
end)
end
end
end
end
local function onUnequipped()
if humanoid_connection then
humanoid_connection:Disconnect()
end
end
--[[-----------------------------------------------------------------------------------
>> MAIN FUNCTION
-------------------------------------------------------------------------------------]]
local Enabled = true
local Last_Attack = 0
local function onActivated()
if not Enabled then
return
end
Enabled = false
local Cooldown = Run.Stepped:wait()
if (Cooldown - Last_Attack < 0.2) then
Lunge()
else
Slash()
end
Last_Attack = Cooldown
damage = Idle_Damage
Enabled = true
end
--[[-----------------------------------------------------------------------------------
>> CONNECTIONS
-------------------------------------------------------------------------------------]]
Tool.Equipped:Connect(onEquipped)
Tool.Unequipped:Connect(onUnequipped)
Tool.Activated:Connect(onActivated)
Sword.Touched:Connect(onTouched)
-- ₪ Kolbias May 12, 2017 Edited June 2, 2017```
Can someone please help?
I’m mostly online on Discord and can respond earlier. Send a friend request if you can help me there: .kinglycidas