Hi! I am try to make a battle royale type game, I have made weapons that can kill players and NPCs.
I am trying to add a team mode, where I separate people to different teams, NPCs are not on a team though. when I did it though, I can only kill players, but not NPCs with this code, the “local team” is an object value.
--Variables for the sword and booleans
local sword = script.Parent
local canAttack = true
local canDamage = false
--onTouch function that runs when any part touches the Blade of the sword
function onTouch(part)
local character = part.Parent
local Ai = part.Parent:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
if game.Workspace.TEAM.Value == true then
if sword.Parent:IsA("Player") then
local team = script.Parent.Team
local parentTeam = sword.Parent
local playerself = game.Players:GetPlayerFromCharacter(parentTeam)
team.Value = playerself.Team
if player.Team ~= team.Value then
if player or Ai then
if canDamage == true then
if not character:FindFirstChild("Melee Shield") then
character.Humanoid:TakeDamage(10)
end
end
end
end
end
end
--check if player exists and can damage is true, then deal the damage
if game.Workspace.TEAM.Value == false then
if player or Ai then
if canDamage == true then
if not character:FindFirstChild("Melee Shield") then
character.Humanoid:TakeDamage(10)
end
end
end
end
end
sword.AxModel.Part.Touched:Connect(onTouch)
and this is the changed code to try to fix it. The result of this code is I can kill NPCs, but not players in team mode anymore. I changed the NPC’s humanoid name from Humanoid to AiHumanoid if that makes a difference.
--Variables for the sword and booleans
local sword = script.Parent
local canAttack = true
local canDamage = false
--onTouch function that runs when any part touches the Blade of the sword
function onTouch(part)
local character = part.Parent
local Ai = part.Parent:FindFirstChild("AiHumanoid")
local player = game.Players:GetPlayerFromCharacter(character)
if game.Workspace.TEAM.Value == true then
if sword.Parent:IsA("Player") then
local team = script.Parent.Team
local parentTeam = sword.Parent
local playerself = game.Players:GetPlayerFromCharacter(parentTeam)
team.Value = playerself.Team
if player then
if player.Team ~= team.Value then
if canDamage == true then
if not character:FindFirstChild("Melee Shield") then
character.Humanoid:TakeDamage(5)
end
end
end
end
end
if Ai then
if canDamage == true then
if not character:FindFirstChild("Melee Shield") then
character.AiHumanoid:TakeDamage(5)
end
end
end
end
--check if player exists and can damage is true, then deal the damage
if game.Workspace.TEAM.Value == false then
if player then
if canDamage == true then
if not character:FindFirstChild("Melee Shield") then
character.Humanoid:TakeDamage(5)
end
end
end
if Ai then
if canDamage == true then
if not character:FindFirstChild("Melee Shield") then
character.AiHumanoid:TakeDamage(5)
end
end
end
end
end
sword.Blade.Touched:Connect(onTouch)
The bottom line is, what happened between the two scripts, with the first one gave me the result of not being able to kill the npcs, to the second result, where I can kill the npcs but not the players?