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

What is wrong with this simple script?

Hi there! What is wrong with this simple script? Thanks in advance!

TeamColor = BrickColor.new("Bright blue") ---For this script to work, i need --the exact color of the team that may touch it only replace the stuff inside ""

script.Parent.Touched:connect(function(hit)
	print(hit)
if hit.Parent.TeamColor == BrickColor.new("Bright blue") then	
	hit.Parent.Humanoid.Health = 0
	else return
end
end)

1 answer

✓ Accepted answer

The Character (the physical component of the Player) doesn't have a TeamColor property. It's just a Model.

In addition, you currently aren't doing a check to prevent errors involving the possible non-existence of hit.Parent or of hit.Parent.Humanoid.


We have to get the Player object corresponding to the Character and check its TeamColor.

script.Parent.Touched:connect( function(hit)
	if hit.Parent then
		-- Check to prevent errors
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player and player.TeamColor == TeamColor then
			-- It actually is a Player (not some other object)
			-- and they are on the `TeamColor` colored team
			character.Humanoid.Health = 0
		end
	end
end )

6 comments

0

Thank you - but it doesn't work :(

0

what doesn't work?

0

Don't worry :) I'll work it out!

0

Well, actually - i'm not sure - i thought I'd solved it because it wanted a name for the function so i did function die(hit) but it still doesn't work :(

0

Sorry, I only posted the function itself without the connection, but forgot to mention it. I edited it to include the connection correctly now

0

Thanks!

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