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

Race positioning, i do not know if this is possible? [COMPLEX]

Asked by 5 years ago

I have made a Go karting racing system fully functioning but i only made it to notice who comes first and i do not know how i will do it for who ever comes other places.

if you can also explain how i can make a folder that has players names with what position they are in i will be very happy.

Sorry if i did not explain well.

local RaceModule = {}
local Checkpoint = require(script.Checkpoint)
local Racer = require(script.Racer)

local GameSettings = game.Workspace.Game.GameSettings

-- Local variable defaults
local startCheckpoint = nil
local finishCheckpoint = nil
local autoStart = false
local debugEnabled = true

local numLaps = 1
local raceDuration = GameSettings.RaceDuration.Value
local intermissionDuration = GameSettings.IntermissionDuration.Value

-- Other local variables
local numCheckpoints = 0
local checkpoints = {}
local racers = {}
local FirstPlace = nil
local SecondPlace = nil
local ThirdPlace = nil

local RacersNeeded = 3 ---WIP

-- Private functions
local function getCheckpointFromPart(part)

    for _, checkpoint in pairs(checkpoints) do
        if checkpoint.CollisionBody == part then
            return checkpoint
        end
    end

    return nil

end

local function hasPlayerFinishedLap(player, lap)

    for _, checkpoint in pairs(checkpoints) do
        if checkpoint:PlayerLapPassed(player, lap) ~= lap then
            return false
        end
    end

    return true

end

local function addRacer(player)
    racers[player] = Racer.new()
end

local function checkAllFinished()
    for _, racer in pairs(racers) do
        if not racer:HasFinishedLap(numLaps) then
            return false
        end
    end

    return true
end

-- Getters/Setters
function RaceModule:SetStartCheckpoint(checkpoint)
    startCheckpoint = getCheckpointFromPart(checkpoint)
end

function RaceModule:GetStartCheckpoint()
    return startCheckpoint
end

function RaceModule:SetFinishCheckpoint(checkpoint)
    finishCheckpoint = getCheckpointFromPart(checkpoint)
end

function RaceModule:GetFinishCheckpoint()
    return finishCheckpoint
end

function RaceModule:SetNumLaps(number)
    numLaps = number
end

function RaceModule:GetNumLaps()
    return numLaps
end

function RaceModule:SetAutoStart(autostart)
    autoStart = autostart
end

function RaceModule:GetAutoStart()
    return autoStart
end

function RaceModule:SetRaceDuration(duration)
    raceDuration = duration
end

function RaceModule:GetRaceDuration()
    return raceDuration
end

function RaceModule:SetIntermissionDuration(duration)
    intermissionDuration = duration
end

function RaceModule:GetIntermissionDuration()
    return intermissionDuration
end

function RaceModule:SetDebugEnabled(enabled)
    debugEnabled = enabled
end

function RaceModule:GetDebugEnabled()
    return debugEnabled
end

-- Events
local onRaceStartedEvent = Instance.new("BindableEvent")
RaceModule.RaceStarted = onRaceStartedEvent.Event 

local onRaceFinishedEvent = Instance.new("BindableEvent")
RaceModule.RaceFinished = onRaceFinishedEvent.Event

local onLapStartedEvent = Instance.new("BindableEvent")
RaceModule.LapStarted = onLapStartedEvent.Event

local onLapFinishedEvent = Instance.new("BindableEvent")
RaceModule.LapFinished = onLapFinishedEvent.Event

local onIntermissionStartedEvent = Instance.new("BindableEvent")
RaceModule.IntermissionStarted = onIntermissionStartedEvent.Event

local onCheckpointPassedEvent = Instance.new("BindableEvent")
RaceModule.CheckpointPassed = onCheckpointPassedEvent.Event

-- Public functions
function RaceModule:SetRacers(racerList)
    for i = 1, #racerList do
        addRacer(racerList[i])
    end
end

function RaceModule:RegisterCheckpoint(checkpoint)
    if not checkpoints[checkpoint] then

        local newCheckpoint = Checkpoint.new(checkpoint, RaceModule)
        numCheckpoints = numCheckpoints + 1
        checkpoints[checkpoint] = newCheckpoint
    end
end

function RaceModule:PlayerCrossedCheckpoint(player, checkpoint)
    local racer = racers[player]    

    if not racer or racer.FinishedRace then return 
    end

    local currentLap = racer.CurrentLap

    if checkpoint == startCheckpoint and not racer:HasStartedLap(currentLap) then
        racer:StartLap(currentLap)
        onLapStartedEvent:Fire(player, currentLap)
    end

    if racer:HasStartedLap(currentLap) then

        local alreadyPassed = checkpoint:SetPlayerPassed(player, currentLap)

        if not alreadyPassed then       
            onCheckpointPassedEvent:Fire(checkpoint.CollisionBody)
        end
    end

    if checkpoint == finishCheckpoint and hasPlayerFinishedLap(player, currentLap) then

        local lapTime = racer:FinishLap(currentLap)

        onLapFinishedEvent:Fire(player, currentLap, lapTime)

        if currentLap == numLaps then
            if not FirstPlace then
                FirstPlace = player
            end

            racer.FinishedRace = true

            if checkAllFinished() then
                onRaceFinishedEvent:Fire(FirstPlace)
            end
        else
            racer.CurrentLap = racer.CurrentLap + 1
        end
    end
end

function RaceModule:Start()

    if debugEnabled then print("RACEMODULE: Starting race")

        FirstPlace = nil
        SecondPlace = nil
        ThirdPlace = nil

        assert(numCheckpoints > 0, "No checkpoints registered")
        assert(startCheckpoint, "Start Checkpoint not set")
        assert(finishCheckpoint, "Finish Checkpoint not set")

        if debugEnabled then 
            print("RACEMODULE: Checking if players set") 
        end

        if #racers == 0 then
            RaceModule:SetRacers(game.Players:GetPlayers())
        end 

        for _, checkpoint in pairs(checkpoints) do
            checkpoint:Reset()
        end

        onRaceStartedEvent:Fire()

        local raceOver = false
        local finishedConnection

        finishedConnection = RaceModule.RaceFinished:connect(function()

            raceOver = true
            racers = {}
            finishedConnection:disconnect()
        end)

        spawn(function()
            wait(raceDuration)
            if not raceOver then
                onRaceFinishedEvent:Fire(FirstPlace)
            end
        end)
    end
end

-- Autostart code
spawn(function()
    wait(5)

    repeat
        onIntermissionStartedEvent:Fire()
        wait(intermissionDuration)

        RaceModule:Start()

        RaceModule.RaceFinished:wait()
    until autoStart == false    
end)

return RaceModule

Thanks For reading it, Comment if you know how i should go about doing this

0
Is it possible for you to include only code that might be relevant? 261 lines is hard to read through plus with how you have your functions organized. xPolarium 1388 — 5y
1
Did you make this script? User#5423 17 — 5y
1
Also something I was wondering. xPolarium 1388 — 5y
0
I used the race script module and changed the script aleandroblingo -35 — 5y

1 answer

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

At first glance (A long one too) what I see going on in your script is that you're not doing anything with how you pass those coming in first place.

You also don't have a function that connects to the fired event on line 226, but I'll just think you're still working on this.

In a condensed version of what you should do:

--Say we want to print some text out with a custom function.
--We pass values with the parameters we give them


--At the top of my example code:
local module = {} --Some table we store our bindable functions in.

local bindable = Instance.new("BindableEvent")
module.Finished = bindable.Event --Basic style of oop

Now we create function we use to fire our bindable event with a parameter to hold the value we want to send.

--'text' is the parameter here.
--This is where your 'issue' comes in
--In your code, you don't do anything with what you pass on line 241
module.Finished:Connect(function(text)

    --Get what ever text is and simply print it
    print(text)
end)

--Function with a good name
function p(text)
    --pass what ever text is
    bindable:Fire(text)
end

p("hello world") --output: hello world

Let me know if this explanation helped. Not a full answer since you seem to have incomplete things.

0
eh, isnt that what i did? aleandroblingo -35 — 5y
0
You're not doing what I did in my module.Finished connection. Look at your code on line 241. You pass who ever is first but never do anything with them on line 231. Compare my example to that. xPolarium 1388 — 5y
0
OH, so should i make a new function is what you are suggesting? aleandroblingo -35 — 5y
0
Sorry for being annoying ;/ aleandroblingo -35 — 5y
Ad

Answer this question