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

I'm trying to make a winner announcement for my racing project any help?

Asked by 4 years ago
Edited 4 years ago

Okay, so I've made a very short but long part that a player's car goes over it they get an announcement saying they've won. Unfortunately this does not work for some reason.

1workspace.win.Touched:Connect(function(hit)
2        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
3        local team = game:GetService("Teams")
4        if player then
5        script.Parent.Winner.Text = "Winner is: ".. player.Name
6        print("hit")
7    end
8end)
1
you never told us the problem zadobyte 692 — 4y
0
@iiMegabyteGaming Thank you Did not notice. charlesec 36 — 4y

1 answer

Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

To make it detect it if a car hit the winner's part, we'll have to use the GetTouchingParts method. Please note that this does not count objects that have their CanCollide property set to false.

Anyways, let's begin. First, we'll have to detect the touching parts, to do this, let's use GetTouchingParts.

1local results = workspace.win:GetTouchingParts() -- gets the touching parts
2print(results) -- results is the touching part(s)

Once we do that, we'll have to detect if there is a Vehicle Seat inside of one of the Touching Parts. To do this, we can use i, v in pairs and GetDescendants and use FindFirstChildWhichIsA to find if it exist.

Code:

1for i, v in pairs(results:GetDescentants()) do -- all the parts inside of all the touching parts
2    local Seat
3    v:FindFirstChildWhichIsA("VehicleSeat") -- detects if a part inside all the touching parts is a Vehicle Seat
4    if Seat then
5    end
6end

Once we do that, we'll have to detect who is inside of the seat. To do this, we can use: Seat.Occupant.Parent, which is the character and GetPlayerFromCharacter. (Also, we'll need to detect if they are a player and the change the Winner Text.

Code:

1if Seat.Occupant ~= "" then -- if there is an occupant then
2    local player = game.Players:GetPlayerFromCharacter(Seat.Occupant.Parent)
3    if player then
4        script.Parent.Winner.Text = "Winner is: " .. player.Name
5    end
6end

Outstanding job! Now, I'll combine the scripts for you.

01local results = workspace.win:GetTouchingParts() -- gets the touching parts
02print(results) -- results is the touching part(s)
03for i, v in pairs(results:GetDescentants()) do -- all the parts inside of all the touching parts
04    local Seat
05    v:FindFirstChildWhichIsA("VehicleSeat") -- detects if a part inside all the touching parts is a Vehicle Seat
06    if Seat then
07        if Seat.Occupant ~= "" then -- if there is an occupant then
08            local player = game.Players:GetPlayerFromCharacter(Seat.Occupant.Parent)
09            if player then
10                script.Parent.Winner.Text = "Winner is: " .. player.Name
11            end
12        end
13    end
14end

Please Accept if it works.

Ad

Answer this question