Need some help with VehicleSeat Script

Hello!
I have this script for a vehicle where if a player doesn’t have enough cash, he gets kicked out of the seat. How do I make it so if the player doesn’t have enough cash, instead of being kicked, the player just cant enter the seat in the first place?

Here’s the script:

local vehicleSeat = script.Parent
local entryFee = 35
local playersWhoPaid = {}

vehicleSeat.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        -- Check if the player has already paid
        if not playersWhoPaid[player.UserId] then
            -- Assuming there's a leaderstats Cash value for the player
            local cash = player:FindFirstChild("leaderstats"):FindFirstChild("Cash")
            if cash and cash.Value >= entryFee then
                cash.Value = cash.Value - entryFee
                playersWhoPaid[player.UserId] = true
                print(player.Name .. " has paid the entry fee.")
            else
                print(player.Name .. " does not have enough cash to enter.")
                -- Prevent the player from sitting if they haven't paid
                vehicleSeat.Disabled = true
                task.wait(1) -- Wait for half a second before re-enabling the seat to avoid spamming
                vehicleSeat.Disabled = false
            end
        else
            print(player.Name .. " has already paid.")
        end
    end
end)
2 Likes

I think you need a localscript so you can disable the seat and leave it disabled, but it will only be for that player since it is a local script.

1 Like

Wrote this code but for some reason the player just stops and im unable to move if I dont have enough cash. Any possible solutions to this bug?

local vehicleSeat = script.Parent
local cashRequirement = 35
vehicleSeat.Disabled = true -- Initially disable the vehicle seat

-- Function to check and handle the purchase
local function handlePurchase(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    local cash = leaderstats and leaderstats:FindFirstChild("Cash")
    
    if cash and cash.Value >= cashRequirement then
        cash.Value = cash.Value - cashRequirement -- Deduct the cash
        vehicleSeat.Disabled = false -- Enable the vehicle seat for use
        script:Destroy() -- Destroy this script to prevent further purchases
    else
        player:Kick("Not enough cash to purchase the vehicle seat.")
    end
end

-- Connect the function to the event
vehicleSeat.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        handlePurchase(player)
    end
end)

It is because the player:Kick() Kicks the player out of the game and not the vehicle seat.

Just apply :Sit(nil) to the seat or disable the sit of the player’s humanoid

1 Like