Hello So I’m making a car tyoe game and there is car spawner part (E prompt to spawn car)
so there is a problem I’m facing right now. It is like I want to make if a player already have spawned
the car, they can’t spawn another car until the car moves far away.
because or else people are gonna spawn so many cars and break the game, I did the time delay inside of Prompt but still if they wait a little bit and spawn over and over again it will cause a big problem.
please tell me a solution for that, how can I make that script to not spawn another car if the currect car haven’t gone far away.
This is the scripting I’m using :
local model = game.ReplicatedStorage.Carts
local cart = model.Cart:Clone()
local button = script.Parent
local sound = button.Sound
local regenerated = false
local ReloadTime = 10
script.Parent.ProximityPrompt.Triggered:connect(function()
if regenerated == false then
-- Notify the player via sound
regenerated = true
button.BrickColor = BrickColor.new("Bright red")
sound:Play()
-- Spawn a cart
local newcart = cart:Clone()
newcart.Name = "Cart"
newcart.Parent = workspace.SpawnedCarts
newcart:MakeJoints()
-- Cooldown
wait(ReloadTime)
-- Reloaded
regenerated = false
button.BrickColor = BrickColor.new("Dark green")
else
-- Don't do anything
return
end
You can use magnitude to get the position between the starting position and the car and then only let the car spawn if the magnitude is higher than any number you want.
local carPosition: Vector3 = newcart.Position
local carSpawnPosition: Vector3 = Vector3.new(0, 0, 0) --replace with the position where your cars spawn.
local magnitude: number = (carSpawnPosition - carPosition).Magnitude
print(magnitude) --This will be the distance between the car and it's spawnpoint
This code should work. I only wrote code, I did not replicate the whole car system for testing purposes.
Make sure the ‘‘cart’’ has a PrimaryPart set.
local carts: Model = game:GetService("ReplicatedStorage").Carts
local cart: Model = carts:FindFirstChild("Cart"):Clone() :: Model
local button: Part = script.Parent
local sound: Sound = button:FindFirstChild("Sound") :: Sound
local regenerated: boolean = false
local reloadTime: number = 10
local proximityPrompt: ProximityPrompt = button:FindFirstChild("ProximityPrompt") :: ProximityPrompt
local primaryPart: BasePart? = if cart.PrimaryPart then cart.PrimaryPart else nil
if not primaryPart then error("PrimaryPart not found!") return end
local cartOriginPosition: Vector3 = primaryPart.Position
local function onProximityPromptTriggered()
local positionB: Vector3 = primaryPart.Position
local magnitude: number = (positionB - cartOriginPosition).Magnitude
if magnitude < 10 then return end --number in studs
if regenerated then return end
regenerated = true
button.BrickColor = BrickColor.new("Bright red")
sound:Play()
cart = carts:FindFirstChild("Cart"):Clone() :: Model
cart.Name = "Cart"
cart.Parent = workspace.SpawnedCarts
cart:MakeJoints()
task.wait(reloadTime) --Always use task.wait()
regenerated = false
button.BrickColor = BrickColor.new("Dark green")
end
proximityPrompt.Triggered:Connect(onProximityPromptTriggered)
Alright, I will try and reproduce the whole thing including the cars to make sure it works. I’m gonna have to sleep right now, I hope I have time tommorow afternoon.
Edit:
Might be worth noting that there also is a “server” tab in the developer console that shows any server-side errors, ignore this if you already knew this.
ohh but I can still spawn many carts, I wanted it like , if a cart is already spawned and at the spawn point then another cart cannot be spawned before that existed cart move a little far away
I used Region3 instead of magnitude, it works fine for me whenever I test it.
local ReplicatedStorage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local car: Model = ReplicatedStorage:FindFirstChild("Car") :: Model
if not car then warn("Car not found!") return end
local spawnedCars: Model = Instance.new("Model")
spawnedCars.Name = "SpawnedCars"
spawnedCars.Parent = workspace
local button: Part = script.Parent
local proximityPrompt: ProximityPrompt = button:FindFirstChildOfClass("ProximityPrompt") :: ProximityPrompt
local distanceFactor: number = 10
local reloadTime: number = 1
local regionMin: Vector3 = Vector3.new(button.Position.X - distanceFactor, button.Position.Y - distanceFactor, button.Position.Z - distanceFactor)
local regionMax: Vector3 = Vector3.new(button.Position.X + distanceFactor, button.Position.Y + distanceFactor, button.Position.Z + distanceFactor)
local regenerated: boolean = false
local function onProximityPromptTriggered()
print("onProximityPromptTriggered")
if regenerated then return end
print("not regenerated")
local region: Region3 = Region3.new(regionMin, regionMax)
local partsInRegion = workspace:FindPartsInRegion3(region)
for index: number, basePart: BasePart in partsInRegion do
if not basePart.Parent or basePart.Parent.Name == "Car" then return end
print(partsInRegion)
regenerated = true
button.BrickColor = BrickColor.new("Bright red")
local newCar = car:Clone()
newCar:MakeJoints()
newCar.Parent = spawnedCars
break
end
task.wait(reloadTime)
button.BrickColor = BrickColor.new("Dark green")
regenerated = false
end
proximityPrompt.Triggered:Connect(onProximityPromptTriggered)
I changed some names and values up, but you can change them back to your likings.