Spaceship script drains ALL my RAM causing studio crash

I made a script to make player be able to fly like in space, but i also added a timer that destroys ship if seat is unoccupied for 10 seconds. However, when i seat in the ship, studio starts to drain my RAM completely and causing studio crash.

Studio leaves 2GB of unreserved RAM, roblox consumes 200 more MB, so there’s still a bit of free memory left if don’t count additional 2GB of SWAP memory.

script:

local ship = script.Parent
local seat = ship.cabin.Seat
local hul = ship.hul

local speed = ship:GetAttribute('acceleration')
local steer = ship:GetAttribute('steer')
local maxSpeed = ship:GetAttribute('maxSpeed')

local currentSpeed = 0
local currentRotationX = 0
local currentRotationY = 0

local SELF_DELETE_TIMER = 10
local DELETION_THREAD = nil

function start()
	
	while true do
		hul.CFrame *= CFrame.new(0, 0, -1) * CFrame.Angles(0, currentRotationX, currentRotationY)
		task.wait(.02)
	end
	
end
function SelfRemoval()
	DELETION_THREAD = task.delay(10, function()
		ship:Destroy()
	end)
end
seat.ProximityPrompt.Triggered:Connect(function(plr)
	
	seat:Sit(plr.Character.Humanoid)
	seat.ProximityPrompt.Enabled = false
	
end)
SelfRemoval()
seat.Changed:Connect(function()
	
	local localScript
	local plr
	if seat.Occupant ~= nil then
		
		task.cancel(DELETION_THREAD)
		plr = seat.Occupant.Parent
		localScript = script.shipControllerClient:Clone()
		localScript.Parent = seat.Occupant.Parent
		localScript.Enabled = true
		start()
		
	else
		SelfRemoval()
		
		if plr then
			localScript:Destroy()
		end
		
		seat.ProximityPrompt.Enabled = true
	end
end)

local throttleActive = false
local brakeActive = false
ship.Action.OnServerEvent:Connect(function(plr, action, active)
	
	if action == 'Up' then
		if active == true then
			currentRotationY = steer
		else
			currentRotationY = 0
		end
	elseif action == 'Down' then
		if active == true then
			currentRotationY = -steer
		else
			currentRotationY = 0
		end
	elseif action == 'TurnRight' then
		if active == true then
			currentRotationX = steer
		else
			currentRotationX = 0
		end
	elseif action == 'TurnLeft' then
		if active == true then
			currentRotationX = -steer
		else
			currentRotationX = 0
		end
	elseif action == 'Throttle' then
		if active == true then
			throttleActive = true
			while throttleActive == true and currentSpeed < maxSpeed do
				currentSpeed += 3
				task.wait(.05)
			end
		else
			throttleActive = false
		end
	elseif action == 'Brake' then
		if active == true then
			brakeActive = true
			while brakeActive == true and currentSpeed > 0 do
				currentSpeed -= 3
				task.wait(.05)
			end
		else
			brakeActive = false
		end
	end
	
end)
ship.AttributeChanged:Connect(function(health)
	
	if ship:GetAttribute('health') <= 0 then
		
		local exp = game.ServerStorage.Explosion:Clone()
		exp.Position = ship.hul
		exp.Parent = workspace
	end
	
end)

maybe i coded something bad, maybe this is a bug, I don’t know

Every time you run a function start() there starts new loop and it can take really many memory if you have many loops

I can guess, but maybe your seat is welded to ship, and every time player drives ship, its changes it CFrame => Changed signal runs => a new loop starts that also changes cframe and this grows into a big problem

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.