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

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

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

2 comments

0

This is the script I want to apply it to:

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

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

0

thanks

0

Before you leave, can you accept my answer, we both get Rep.

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".

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

0

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:

  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)

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