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