I want to make a zombie infection game where there are to separate teams of people and I want it to where when somebody gets touched they change teams and turn green does anybody know how to do this?
I feel like this topic wouldn’t have a need to be created if you would’ve simply done a little research beforehand. Not all the blame to you, just try and look something up before making a new topic about it on the DevForum. I’ve found that a LOT of problems I’ve had, have already been answered somewhere on the DevForum
Here’s a link to Touched Events from parts and Touched Events from Humanoids
function changeTeams(part)
if game:GetService(“Players”):GetPlayerFromCharacter(part.Parent) then
game:GetService(“Players”):GetPlayerFromCharacter(part.Parent).Team = TEAMHERE
end
end
Part.Touched:Connect(changeTeams)
- Ignore the poor indentation, I’m on mobile.
Also how would I make them turn green on the other team?
There is something called a .Touched Event that would prove to be of use here, try searching it up.
It is an event that exists for a Base Part when it comes into contact with anything else, physically.
You can use this event to detect what the object that hit actually was, allow this code to demonstrate how to get the name of the part/object that comes in contact with a part called “Part” and print it.
local Part = workspace:WaitForChild("Part",7)
Part.Touched:Connect(function(object)
print(tostring(object.Name))
end)
How would I make the character who got touched turn green?
Often inexperienced developers just ask for free code and others just throw them at the OP for a solution, without explaining anything. This is completely useless if it is not explained what the lines of code actually do.
Since this is not a venue for code to be given away, I will only give an outline:
You use the .Touched Event on a part and detect the object that touched, if it was a descendant of the character that touched the part, use this as a check (use an if statement) to ensure that the code runs only if the Character is an ancestor of the part, or the part is a descendant of the Character, or otherwise unnecessary code would run for objects that aren’t part of the player.
Now that you have located the Character, simply use a generic loop to iterate over the Character’s children and if they are base parts, change all of their Part.Color
properties en masse to BrickColor.New(37) or a code of your desire.
Here is a list of BrickColor codes that might help you choose a code.
There is something called a Touche Event that could maybe help you scripting the zombies or people that will be attacking in-game players.
This is very simple, and could have been done with very minimal research. For the sake of this has already been posted, start with
part.Parent.Touched:Connect(function(part)