Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How would I make this script work?

Asked by
Avectus 120
9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I'm trying to make a sword and the code below is in a regular script, inside the sword.

I want it to be that if you hit an enemy with the sword the enemy is damaged by whatever damage is in the script (obviously) but I don't want the sword to deal damage to you or your teammates.

damage = 5

Saber2 = script.Parent.Saber2

function DealDamage()

end

Saber2.Touched:connect(DealDamage)

I'm not sure what I should put in the DealDamage function, or if that's the best way to go around doing this.

Could someone tell me what I need to put in the function or if there is a better way to be doing this?

Thanks.

1 answer

Log in to vote
0
Answered by 9 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

First thing you'd want is to check if the item touched is a player then check if they are in your team than damage if false here is the function you should use. Assuming the script is inside the character.

local damage=5
local player=game.Players:GetPlayerFromCharacter(script.Parent)
function Touched(hit)
local hum=(hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")) or nil
if hum~=nil then
local plyr=game.Players:GetPlayerFromCharacter(hum.Parent)
if plyr~=nil then
if plyr.TeamColor~=player.TeamColor then
hum:TakeDamage(damage)
end
end
end
end

Please rep if you like this and yes I know it can be shortens but this guy won't get it if it is.

0
This code is barely readable... BlueTaslem 18071 — 9y
0
Again on my phone can't do much about it And can you guys really stop rep-Ing me down cause it ry to help on my phone nstrike159 15 — 9y
Ad

Answer this question