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

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

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)

2 comments

1

you never told us the problem

0

@iiMegabyteGaming Thank you Did not notice.

1 answer

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.

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