Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Anti-TeamKiller script does not damage NPCs?

Hello,

EDIT: Original anti-TK script was sorted out, reposting the now correct script with the new weird issue.

I have a sword script, thanks to user ScrewDeath, I was able to fix the error. But now, I can not inflict damage on NPCs who all have the "Humanoid" property. Do I need to set them to a team?

Error is: 00:42:31.079 - Players.Player1.Backpack.BasicSword.SwordScript:6: attempt to index local 'TargetPlayer' (a nil value)

Damage section of the Sword Script:

handle.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local TargetPlayer = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	if equipped and character and humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(character) then
		if  humanoid and  humanoid.Health > 0 and not hithumanoids[humanoid] and (TargetPlayer.TeamColor ~= player.TeamColor or not TargetPlayer)
			then
			hithumanoids[ humanoid] = true
			
			for _, v in pairs( humanoid:GetChildren()) do
				if v and v.Name == "creator" then
					v:remove()
				end
			end
			local tag = Instance.new("ObjectValue")
			tag.Name = "creator"
			tag.Value = player
			debris:AddItem(tag, 3)
			tag.Parent =  humanoid
			 humanoid:TakeDamage(damage * (combo + 1))
			else
		
		end	
	end	
	end
end)

2 comments

0

Why do you create an ObjectValue for no reason?

0

For the damage portion. :) Do you have thoughts on the line I pointed out where the TeamColor is wrong?

1 answer

✓ Accepted answer

First thing you should look at when rubber ducking is look and comprehend the actual error you are being given: "TeamColor is not a of Humanoid". I'm just going to assume you left out 'member' or something off that error.

So, TeamColor is apparently not a valid property of Humanoid. Bam. There's your problem. You're tryna read something that don't exist. What you would probably want to do is use the humanoid's parent (or the character model) 's name and game.Players:GetPlayer(model.Name) that. That'll give you the player object which is the thing you're looking for.

PS: You say it's FilteringEnabled but I don't see a call to a remoteFunction/Event anywhere. Haven't really tinkered w/ that stuff much so I might be wrong...

14 comments

0

Thanks for the response it helped me clear the cobwebs, yes, word left out was my fault. Got around that with game.Players:GetPlayerFromCharacter(humanoid.Parent) which successfully allowed me to not hurt team members while still inflicting damage on the opponent. The script is FE, I only included one small section of it and mentioned it was FE, in case suggestions offered were to the contrary.

0

Strangely now though, players are unable to damage NPC characters, which is weird as all the NPCs have the "Humanoid" property too. Is it because they're not on a team?

1

That's probably the problem. A way around that would just to alter the condition so it's if (playercolors match) OR (no player exists)

0

Very weird, how would you go about scripting that? I edited my original post with the updated, working, script. Thank you again for your thoughts!

1

This should work "... and (TargetPlayer.TeamColor ~= player.TeamColor or not TargetPlayer)"

0

Hmm, I added that, and I get this error from Line 3 when attacking an NPC: 00:27:30.354 - Players.Player1.Backpack.BasicSword.SwordScript:3: attempt to index local 'humanoid' (a nil value)

1

You probably attacked a non-NPC that doesn't have a humanoid. Prolly should surround the whole thing with an "if humanoid then" right after you define humanoid

0

Negative, still unable to hurt NPC's that have "Humanoid" as a property. I updated the first post, cleaned up some of the duplicate variables there. Error line for when hitting NPCs is now: Error is: 00:42:31.079 - Players.Player1.Backpack.BasicSword.SwordScript:6: attempt to index local 'TargetPlayer' (a nil value)

0

TargetPlayer is nil, what do you do? make sure you try to check that is nil before you access stuff from it (e.g "TargetPlayer.TeamColor")

1

Also make sure you're adding parentheses to the condition like I did above to ensure that it's executed in order

0

Thanks for the response! I added the parentheses , edited the first post again to show, still getting the same error, TargetPlayer is nil. I got nada, NPCs stopped receiving damage after I specified the TeamColor stuff.

0

I'm unsure why TargetPlayer is nil, when TargetPlayer is grabbing the humanoid from the model, and all my NPCs have the standard "Humanoid" property in them, and were receiving damage prior.

1

In the end prolly not the most efficient approach, but all you need is to add ... (TargetPlayer and TargetPlayer.TeamColor ~= player.TeamColor or not TargetPlayer)

0

SUCCESS. :D thank you very kindly good sir.

This discussion is preserved as it was. New answers, comments, and votes are closed.