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.

workspace.win.Touched:Connect(function(hit)
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        local team = game:GetService("Teams")
        if player then
        script.Parent.Winner.Text = "Winner is: ".. player.Name
        print("hit")
    end
end)
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.

local results = workspace.win:GetTouchingParts() -- gets the touching parts
print(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:

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

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:

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

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

local results = workspace.win:GetTouchingParts() -- gets the touching parts
print(results) -- results is the touching part(s)
for i, v in pairs(results:GetDescentants()) do -- all the parts inside of all the touching parts
    local Seat
    v:FindFirstChildWhichIsA("VehicleSeat") -- detects if a part inside all the touching parts is a Vehicle Seat
    if Seat then
        if Seat.Occupant ~= "" then -- if there is an occupant then
            local player = game.Players:GetPlayerFromCharacter(Seat.Occupant.Parent)
            if player then
                script.Parent.Winner.Text = "Winner is: " .. player.Name
            end
        end
    end
end

Please Accept if it works.

Ad

Answer this question