Hello, here I am programming a football game and I would like to make a touch, there are two teams team 1 and team 2, there is a red line which indicates the touch, I would like that when a person from team 1 kicks the ball and the ball crosses the red line, I would like the touch to be for team 2.
Then when team 2 kicks the ball and the ball crosses the red line, I would like the touch to be for team 1
When a player kicks the ball, set a value to check which team touched it last. Once it hits the required destination, check if the value is team 1 or team 2.
first you need to make sure that you at least have one Team
to create a Team you need to go to Teams (it should be in the explorer) and create a Team inside of it. Select the team that you’ve created and get to the properties tab and change the TeamColor property to the team color you want.
next make a script and write the following inside it
-- put this script inside the line
-- I explained things as much as I can
local line = script.Parent -- the line that detects the touching
local goal = workspace.Goal -- the goal or the part that changes in colors
local ball = workspace.Ball -- the ball
local function onScored(team) -- fires when a team scores a goal!
-- do anything
print(team.Name .. " scored!") -- this message should be in the output
end
local function onLineHit(hit) -- hit is the part that touched the line
if hit == ball then
local tag = ball:FindFirstChild("TeamTag") -- checks which team kicked the ball
if tag then
local team = game:GetService("Teams"):FindFirstChild(tag.Value)
if team then
goal.BrickColor = team.TeamColor
onScored(team)
end
end
end
end
local function onBallHit(hit) -- hit is the part that touched the ball
if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then -- checks if it's a player
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local team = player.Team
local tag = ball:FindFirstChild("TeamTag")
if tag then
tag.Value = team.Name
else
local tag = Instance.new("StringValue", ball) -- creates a tag inside of the ball
tag.Name = "TeamTag"
tag.Value = team.Name
end
end
end
line.Touched:Connect(onLineHit)
ball.Touched:Connect(onBallHit)