E prompt spawn part

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

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
1 Like

I understand but sorry if I’m being dumb,
I’m a new scripter so I mean where should I put your script in my script? I mean which line?

If you don’t mind can you prepare me the whole script I mean combining your and my script. So I can understand what you meant.

2 Likes

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)
2 Likes

I did all the thing, but now it’s not even spawning the cart for some reason.

Could you tell me what errors occur in the output?

1 Like

There is literally nothing showing in the output, I have added print inside of the script to see if the script is working, it’s not even printing.

1 Like

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.

1 Like

Sorry, my bad Instead of checking if the magnitude was higher than 10, I checked if it was below 10! Resulting in a return end…

Simply change:
if magnitude > 10 then return end

to:
if magnitude < 10 then return end

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

1 Like

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.

1 Like

I don’t know but it still didn’t work for me. if you don’t mind can you add me on roblox and take look at it?
I would really appreciate it.

1 Like

Sure thing, but the same problem as yesterday… sorry… I’ll take a look later, though!

1 Like

Alright no problem, good night! see you tomorrow

1 Like

are you back? also can you add me on discord so we can have chat there instead of here?

I’m back again, I think we can discuss on the Roblox chat.

1 Like

sorry I went offline at that time. are you online right now?

1 Like