Melee weapon no team damage?

I have a very simple sword script and I have been looking for ways to make it check teams before it does damage. Does anyone know an easy way to check teams for damage with this script?

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
	if humanoid then
		humanoid:TakeDamage(2)
	end
end)

Thanks

1 Like

I usually do this:

script.Parent.Touched:Connect(function(a)
   if a:FindFirstAncestorOfClass("Model") and game.Players:FindFirstChild(a:FindFirstAncestorOfClass("Model").Name) and game.Players[a:FindFirstAncestorOfClass("Model").Name].Team ~= game.Players[script.Parent:FindFirstAncestorOfClass("Model").Name].Team then
     a:FindFirstAncestorOfClass("Model").Humanoid:TakeDamage(2)
   end
end)
1 Like

eeeeeeeeee
it doesn’t seem right.
:upside_down_face:

It’s complicated :upside_down_face:

Is that put in a Touched event?
The code could cause an error since “hit” is not defined :slight_smile:
Edit: Sorry for not reading your code properly, I didn’t see “RedSword.Touched”. “game.Players.PlayerAdded” should be replaced by “RedSword.Touched”, maybe

2 Likes

Well, I don’t test it, I hope it might help :

--Put this script [server script] inside the Tool[Roblox premade tool instance]
--make sure your create BasePart called 'Handle' inside the Tool
local tool = script.Parent
local handle = tool.Handle
local Lplayer = nil

tool.Equipped:Connect(function()
	Lplayer = game.Players:GetPlayerFromCharacter(tool.Parent)
end)

tool.Activated:Connect(function()
	--play sword animation,sound
end)

handle.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player == nil or player == Lplayer then return end
	if player.Team.Name ~= Lplayer.Team.Name then --Check if Lplayer[currently using the sword] Team is not equal to player[victim] team
		local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
		if humanoid then
			humanoid:TakeDamage(2)
		end
	end
end)

1 Like

This made it so the sword stops doing damage to everything

I tried this and it doesnt do any damage to anything either.

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
	local teams = game:GetService("Teams")	
	if humanoid ~= teams.teamname then
		humanoid:TakeDamage(2)
		end
end)

local Player --player holding the sword
script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
	local teams = game:GetService("Teams")	
       if humanoid  then
          local hitplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
          if hitplayer ~= nil and hitplayer ~= player then
              if hitplayer.Team ~=  player.Team then
		         humanoid:TakeDamage(2)
              end
          end
      end
end)

This will work, all you need to do is specify the player holding the sword, which I can’t do for you

1 Like

Thanks. What do you mean specify the player holding the weapon? Cant I just make all players who hold it check what team they are on?

local player = game:GetService("Players").LocalPlayer

or

local player = Players.LocalPlayer

Will that work?

Is the script on the client or on the server

I use morphs and the morph has a tool in his hand by default. There are also weapons in world players can buy. The script is in the tool. Not sure what you mean by client or server. I am assuming client .

You can only get the localplayer from a local script