Spawn car sell car

Hey there!

I’m making a spawn car sell car system and need some help.
This is the current code which kinda works, and a brief explanation.


RegenScript

system = script.Parent

model = system.Car --

backup = model:Clone()

regen = system.Regen



function checkRegen()

	if regen.Value == 1 then

		model:remove()

		wait(1)

		model = backup:Clone()

		model.Parent = system

		model:MakeJoints()

	end

end

regen.Changed:connect(checkRegen)

CarSold

local body = script.Parent.Body
local regen = body.Parent.Parent.Regen


while task.wait(1) do


	body.DescendantRemoving:Connect(function(part)
		
		if part.Name == "glass" then
			
			print("Car sold")
			
			wait(1)
						
			regen.Value = 1

			wait(1)

			regen.Value = 0
			
	end
end)
	
	end

KillScript

local event = game.ReplicatedStorage.OpenGui

local sound = game.SoundService.SellSound

script.Parent.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") then
		
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		
		
		event:FireClient(plr)
		
		sound:Play()
		
		wait(2.5)
		
		hit.Parent.Humanoid.Health = 0
		
	end
	
	
	
	
end)

I am using Zednovs tycoon kit and I’m using the dropper PartCollector which checks for parts with a string called “Cash”.
I put the string “Cash” inside the part named “glass”.
When “glass” touches PartCollector it gets destroyed (and I get the Cash.Value).

The CarSold script checks if “glass” has been destroyed/removed.
If “glass” has been destroyed it changes regen.Value to 1.

RegenScript checks regen.Value.
If regen.Value = 1 then it removes the car model and spawns a new car.

KillScript plays a sound, opens a gui, and kills the player.

I need some help with optimizing this mess.
My goal is to have the CollectorPart freely in workspace, with a script that checks for the entire car model unlike my glass workaround.
The cash string should also be

  1. Placed inside the model, instead of a part.
    or
  2. Inside the script with a couple of Instance.new("StringValue"), to set the value, maybe?

I’m very open to ideas and feedback, and will be super grateful if anyone could help me out with this! :slight_smile:

Hello!

I have made some progress with this system which I would like to share.

It now consists of two scripts, placed in the model and in ServerScriptService.

CarCollector

local carCollector = game.Workspace.CarCollector



carCollector.Touched:Connect(function(Part)
	if Part.Name == "Collider" then
		
		local Collider = Part
		local Model = Collider.Parent.Parent
		local Collect = Model.Parent.Parent.CurrencyToCollect
		local Cash = Model.Cash
		
		Collider.Name = "Touched"							-- Changes the name to Touched so it wont touch multiple times
		
		print("Collider Touched")							-- Prints Prints Collider Touched
		
		print("Got " .. Cash.Value)							-- Prints the Cash.Value of the car
		
		Collect.Value = Collect.Value + Cash.Value 			-- Adds the Cash.Value to the current Collect.Value
		
		print("You now have " .. Collect.Value)				-- Prints your new balance
		
		carCollector.Color = Color3.fromRGB(75, 151, 75)
		
		wait(1)
		
		carCollector.Color = Color3.fromRGB(196, 40, 28)
		
		Model.Regen.Value = 1								-- Sets Regen.Value to 1, firing the regen script
	
	end
end)

RegenScript

local Spawner = script.Parent

local Model = Spawner.Car

local Clone = Model:Clone()

local Regen = Spawner.Regen



function CheckRegen()

	if Regen.Value == 1 then

		Model:remove()

		wait(1)

		Model = Clone:Clone()

		Model.Parent = Spawner

		Model:MakeJoints()
		
		wait(5)
		
		Regen.Value = 0
		

	end
end

Regen.Changed:Connect(CheckRegen)

Any feedback? What could be improved/changed? :slight_smile: