if plr.Team ~= target.Team then
targetHumanoid:TakeDamage(damage)
end
Someone gave me this example before but I don’t know how to use it. I just know that I need to use the player service and that I need something that works for most weapons This is a very simple example and I think a more professional version is really needed.
I have different weapons in my game, and each one has different damage. Is there a way to do this in the server script service without having to customize each weapon?
You can write a function that determines whether or not the tool’s user is able to damage a given character
--[[ Variables ]]--
-- Services --
local Players = game:GetService("Players")
-- Tool --
local tool = script.Parent
local userCharacter = nil
--[[ Functions ]]--
local function canDamage(targetCharacter : Model) : boolean
local userPlayer = Players:GetPlayerFromCharacter(userCharacter)
local targetPlayer = Players:GetPlayerFromCharacter(targetCharacter)
--// NPC case
if userPlayer ~= nil or targetPlayer ~= nil then
return true
end
local isTeammate = userPlayer.Team == targetPlayer.Team
if isTeammate then
return false
end
return true
end
local function damageTarget(targetCharacter : Model) : ()
if not canDamage(targetCharacter) then
return
end
targetCharacter.Humanoid:TakeDamage(...)
end
local function onEquipped() : ()
userCharacter = tool.Parent
end
tool.Equipped:Connect(onEquipped)
I would recommend putting a function like that in a module if it’s going to be used by many scripts in your game so it’s easier to make changes without having to go through and edit each script that uses it, but it’s not necessary