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

How do you make this function only apply to one team?

Asked by 8 years ago

I am writing a script for a game where I want players to earn money for stepping on a brick, but for only one team. Please help me

0
This is the script I want to apply it to: Pidude_314 -5 — 8y
0
moneyToGive = 100 debounce = false script.Parent.Touched:connect(function(hit) if debounce == true then return end player = game.Players:GetPlayerFromCharacter(hit.Parent) if player == nil then return end stat = player:findFirstChild("leaderstats") if stat == nil then return end cash = stat:findFirstChild("Cash") if cash == nil then return end debounce = true cash.Value = cash.Value + moneyToGive Pidude_314 -5 — 8y

3 answers

Log in to vote
0
Answered by 8 years ago

Simple, add an if statement for the team color. For example...


game.Workspace.Part.Touched:connect(function(Hit) if hit.Parent:FindFirstChild("Humanoid") then if game.Players:GetPlayerFromCharacter(Hit.Parent ) ~= nil then if game.Players:GetPlayerFromCharacter(Hit.Parent).TeamColor == "Really Blue" then --Code end end end end)
0
thanks Pidude_314 -5 — 8y
0
Before you leave, can you accept my answer, we both get Rep. animorphs30 300 — 8y
0
1. Instead of saying "game.Players:GetPlayerFromCharacter..." twice, you should assign the result to a local variable. 2. TeamColor is not a string, so the code would never execute. You could do "TeamColor.Name". chess123mate 5873 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

game.Workspace.Part.Touched:connect(function(Hit)

if hit.Parent:FindFirstChild("Humanoid") then

    if game.Players:GetPlayerFromCharacter(Hit.Parent ) ~= nil then

        if game.Players:GetPlayerFromCharacter(Hit.Parent).TeamColor == "Lime green" then

moneyToGive = 100 debounce = false script.Parent.Touched:connect(function(hit) if debounce == true then return end player = game.Players:GetPlayerFromCharacter(hit.Parent) if player == nil then return end stat = player:findFirstChild("leaderstats") if stat == nil then return end cash = stat:findFirstChild("Cash") if cash == nil and then return end debounce = true cash.Value = cash.Value + moneyToGive wait(2) debounce = false end)

        end

    end

end

end)

0
that is my final script and it isnt working Pidude_314 -5 — 8y
Log in to vote
0
Answered by 8 years ago

animorphs30 had the right idea.

I've touched up your script, but the only real modifications are:

  1. Definition of specialTeam
  2. Not continuing with the function if the player's .Team is different than specialTeam.
local moneyToGive = 100
local debounce = false
local specialTeam = game.Teams["Special Team"] --change the part in quotations to match the name of the desired team
script.Parent.Touched:connect(function(hit)
    if debounce then return end
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if not player or player.Team ~= specialTeam then return end
    local stat = player:FindFirstChild("leaderstats")
    if not stat then return end
    local cash = stat:FindFirstChild("Cash")
    if not cash then return end
    debounce = true
    cash.Value = cash.Value + moneyToGive
    wait(2)
    debounce = false
end)

Answer this question