Ive tried this a few different ways and I have two scripts based on peoples suggestion but they still don’t accomplish what I need.
This is the swords damage script. The swing animation script disables it when its not being swung at something. It damages players and AI humanoids correctly. But I want it to not damage players on the same team.
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
if humanoid then
humanoid:TakeDamage(2)
end
end)
super simple script.
Here is a solution someone suggested I try but this script makes it so nothing takes damage at all. No monsters damage or player damage regardless of team.
local player = game:GetService("Players")
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)
Anyone have any ideas?
The player variable is the Player Service, that’s why it can’t check its team.
Ill change that line to this. See if that works.
local player = game.Players.LocalPlayer
I changed the script to this and still no damage to monster humanoids.
local player = game.Players.LocalPlayer
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)
Is it a LocalScript? If yes, that would work.
However, that would mean any damage a player does would only happen on their screen, not on the others. To change that, you need to use a RemoteEvent or RemoteFunction to communicate with the server. Let the server handle the damage part.
No its not a local script. So you are saying I need to make a new script with remote events to fix the team damage problem.
Yup. Make the LocalScript handle the Touch Event, and then use :FireServer() (RemoteEvent) or :InvokeServer() (RemoteFunction) to deal the damage.