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
How do you make this function only apply to one team?
3 answers
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)
3 comments
thanks
Before you leave, can you accept my answer, we both get Rep.
- 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".
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)
1 comment
that is my final script and it isnt working
animorphs30 had the right idea.
I've touched up your script, but the only real modifications are:
- Definition of
specialTeam - Not continuing with the function if the player's
.Teamis different thanspecialTeam.
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)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
This is the script I want to apply it to:
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