Help with adding debounce to spawning cars?

Oh yes, I forgot to mention to use pcalls because everything in DataStores requires them.

Capitalising :connect() is not necessary, but it can make the code looking more elegant. In my preferences I usually do the capitalised one over the all lowercase style.

that didn’t work same thing dots are red underlined

remove the other dot, it must be only 2 dots. This is caused by the fomatting of the post.

@anon81993163 Thought :connect was depreciated? Not sure

1 Like

now the debounces are errors if I remove the the third dots the debounce becomes unknown global debounce

Don’t know if this works, but try.

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local datastore = game:GetService("DataStoreService")
local dsl = datastore:GetDataStore("StageSaveSystem")
local DebounceTable = {}

Players.PlayerAdded:Connect(function(plr)
    DebounceTable[plr] = {"debounce" = false}
    local folder =Instance.new("Folder", plr)
    folder.Name = "leaderstats"

    local Cash = Instance.new("IntValue", folder)
    Cash.Name = "Cash"
    Cash.Value = dsl:GetAsync(plr.UserId) or 200
    local success, errorMessage = pcall(function()
        dsl:SetAsync(plr.UserId, Cash.Value)
    end)

    if success then
        Cash.Changed:Connect(function() -- oof'd :Connect()
            local success, errorMessage = pcall(function()
                dsl:SetAsync(plr.UserId, Cash.Value)
            end)
    
            if not success then
                warn(errorMessage)
            end
        end)
    else
        warn(errorMessage)
    end
end)

Players.PlayerRemoving:Connect(function(plr)
    DebounceTable[plr] = nil
end)

replicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)
    return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
end

replicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
    if DebounceTable[player]["debounce"] then
        return -- if it's true, it won't process the cloning
    end
    DebounceTable[player]["debounce"] = true

    local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
    car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
    car.Parent = workspace
    car:MakeJoints()
    car.Name = (player.Name .. "'s " .. NameOfCar)
    wait(5) -- Cooldown
    DebounceTable[player]["debounce"] = false
end)

Count the dots.

@uJordy Hm, I thought they did… but apparently I can’t find the documentation again. :frowning:

1 Like

Huh, try replacing it with debounce only and not "debounce".

This is a trial and error for me, which could benefit my learning experience.

1 Like

it works thanks, but is there a way you can destroy the old car when you spawn a new one or would that be too difficult I know you have to use something like

local oldcar = script.Parent.car

oldcar:destroy()

but yea the debounce works thanks

plus destroying the old car will reduce memory or lag because the old model is gone and you have your new one

You could assign the car to the table too, and then using the table again to acquire the old model and then destroying it.

replicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
    if DebounceTable[player]["debounce"] then
        return -- if it's true, it won't process the cloning
    end
    DebounceTable[player]["debounce"] = true

    local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
    car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
    car.Parent = workspace
    car:MakeJoints()
    car.Name = (player.Name .. "'s " .. NameOfCar)

    if DebounceTable[plr]["Car"] ~= nil then
       DebounceTable[plr]["Car"].Parent = nil
    end
    DebounceTable[plr]["Car"] = car

    wait(5) -- Cooldown
    DebounceTable[player]["debounce"] = false
end)

Side note: You can use another table instead or rename it to something appropriate.

Honestly you should probably check is the vehicle is still in the spawn place as you will still get issues if a vehicle is still there during the spawning.

Maybe you could add some sort of force to push out the vehicle from the spawn so that others can get spawn without delay from an AFK player. (Maybe add some collision groups so that the vehicle can not re-enter?)

That’s just my thoughts on this

1 Like

plr is error

maybe you didn’t add local plr = something

Oopsie daisy, my bad that the variable should be player and not plr. :sleepy:

1 Like

its ok don’t worry no hard feelings just your a great scripter tho

1 Like

oh no now I have my money but the cars don’t have their price which means I cant spawn them

Can I please remind that you have to be proactive too- we can’t spoon feed you to your solutions by just giving out code.

yea I know im not very skilled with scripting I know some things I understand tho

this is my first time using these dev forums im a new member

I have to remind you that to keep all your posts sentences in one and not typing them sentence by sentence(they will flag them for spam).

Okay, so what we have here is a RemoteFunction. Did you invoke the server to acquire the price of the car from the local script?