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

How can I prevent a function from carrying on if the player is on the same team?

Asked by
Xl5Xl5 65
9 years ago

the kill function kills the player when the gui button is clicked, then "if bug" area just checks if the other player that has been hit has been "infected" then it carries out a message then carries out the other player that has been infected death

These two chunks here are relevant to a Players death. The first chunk is the function and the second chunk is the GUI, This all works fine, but I want it able to not kill a Player if the Player is on the same team.

I hope I made sense! Please help

function kill()
if bug == nil then return end
if bug:FindFirstChild("Torso") == nil then
local vCharacter = Tool.Parent
local newPlayer = game.Players:playerFromCharacter(vCharacter)
local message = Instance.new("Message")
message.Text = ""
message.Parent = newPlayer
wait(1)
message.Parent = nil
else
bug.Humanoid.Health = 0    -- This is where it kills player regardless of which team, or even if its an NPC how can I make it so it doesn't kill the people on the same team?
reset()
end
end

Below is the button that if clicked on then the function will happen.

    local button5 = Instance.new("TextButton")
    button5.Name = "Button"
    button5.Text = "Kill"
    button5.BackgroundColor3 = Color3.new(0.752941, 0.752941, 0.752941)
    button5.Size = UDim2.new(0,70,0,25)
    button5.Position = UDim2.new(0,80,0,35)
    button5.Parent = frame
    button5.MouseButton1Click:connect(function() kill() end) -- When the kill function begins

3 answers

Log in to vote
1
Answered by 9 years ago

For players, you could just use an if statement that end the functions if the player's TeamColor property is equal to other player's TeamColor property.

For NPCs, it's a bit more tricky as NPCs usually don't have teams associated with them. You could perhaps add a BrickColor value called TeamColor to the NPC and make that color the infected team's color and then do an if statement that ends the function if the player's TeamColor is equal to the BrickColor value's value.


Code example:

local bugPlayer = game.Players:GetPlayerFromCharacter(bug) --Gets a player from the bug, if there is one.
if bugPlayer then --If there is a player associated with the bug, do this block of code.
    if newPlayer.TeamColor == bugPlayer.TeamColor then return end --If the newPlayer's Team is the same as the bugPlayer's team then stop the function
    --Do stuff here
else --If bug is an NPC, do this block of code instead.
    if bug:FindFirstChild("TeamColor") and bug.TeamColor.Value == newPlayer.TeamColor then return end --If the newPlayer's Team is the same as the bug's team then stop the function
    --Do stuff here
end

I hope my answer helped you. If it did, be sure to accept it.

0
I have tried that, but it fails to work, i changed it around too function kill() if bug == nil then return end if bug:FindFirstChild("Torso") == nil then local vCharacter = Tool.Parent local newPlayer = game.Players:playerFromCharacter(vCharacter) local message = Instance.new("Message") if bug then if newPlayer.TeamColor == bug.TeamColor then return end --Do stuff here else if bug:Find Xl5Xl5 65 — 9y
0
Here is a link to full script http://pastebin.com/Pdmh8Q8x Xl5Xl5 65 — 9y
0
You need to add a BrickColor value to the NPC called "TeamColor" without the quotes. Spongocardo 1991 — 9y
0
Still dont work, I put in a BrickColor value inside an NPC renamed the Value TeamColor also made sure the team color underneath is not the same as the team I am in Xl5Xl5 65 — 9y
0
Follow the code example carefully. I check to see if the bug is connected to a player and then run one code block and if it's not, I run another code block. Spongocardo 1991 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Well, in my situation I would've placed an IntValue or NumberValue in Workspace for every team, and then I'd just make it check if it was == 1, and, if not, set it to 1, then allow the script to continue. From there, I'd end with else wait() end

This is just a simple solution to your problem

0
Could you talk me through how to do that? Xl5Xl5 65 — 9y
Log in to vote
0
Answered by
Nosteal 50
9 years ago

Pretty much all you gotta do is make a script that creates a NumberValue for every person that joins the game. if that person is on a certain team, a NumberValue (Let's say it's called Team) will be 1. If that person is not on that team, the value will be 0. If the value is 1, the player will be killed. If the value is 0, well the player won't die.

Answer this question