Deleting a local script when someone jumps out of a seat

I’m working on a cruiser main gun and I’m trying to delete the local script controlling the F to fire function.

I’m hoping I can use my looping script for this…

The script:

local seat = script.Parent
local NoPlayerError = "None is in the seat..."

while wait(0.5) do
	if seat.Occupant ~= nil then
		local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
		local printmsg = plr:WaitForChild("PlayerGui")
		local localcontroller = script.Controller
		local controllerclone1 = localcontroller:Clone()
		controllerclone1.Parent = printmsg
		
		end
	end
	if seat.Occupant == nil then
		print(NoPlayerError)
end

Thanks, I hope someone figures this out soon.

– Luke

Firstly, you are missing an end right after the last if statement, it’s above the if statement instead of below it.

   if seat.Occupant == nil then
		print(NoPlayerError)
   end

Secondly, you can use a GetPropertyChangedSignal event, on the occupant, instead of the loop.

seat:GetPropertyChangedSignal("Occupant"):Connect(function()

This fires whenever the “Occupant” property is changed

https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal

1 Like

also I don’t see why “else” wasn’t used

I checked if I was missing any ends, it appears I wasn’t. I’m a very over-complicated programmer and like to do some weird things with my code but it works. What about deleting the local script?

Agreed, the second if statement isn’t needed, as you suggested. Instead, you can have an else in the first if statement, as your first if statement checks if its not nil, then if it is, the else statement will cover that:

local seat = script.Parent
local NoPlayerError = "None is in the seat..."

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
   if seat.Occupant ~= nil then
     local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
     local printmsg = plr:WaitForChild("PlayerGui")
     local localcontroller = script.Controller
     local controllerclone1 = localcontroller:Clone()
     controllerclone1.Parent = printmsg
  else
    print(NoPlayerError)
   end
end)

Well it depends where the local script is located, you can delete it from the server using the :Destroy() function.

The script is put inside of the PlayerGui inside the player, is there anyway to delete it in the ‘else’?

Nevermind, I figured it out.

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant ~= nil then
		local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
		local printmsg = plr:WaitForChild("PlayerGui")
		local localcontroller = script.Controller
		local controllerclone1 = localcontroller:Clone()
		controllerclone1.Parent = printmsg
		seat:GetPropertyChangedSignal("Occupant"):Connect(function()
			if seat.Occupant == nil then
				controllerclone1:Destroy()
			end
		end)
	else	
		
	end
end)

Nice, just for future reference, the code can be cleaned up a bit:

Instead of needing another GetPropertyChangedSignal, just use an else statement, as in this line:

if seat.Occupant ~= nil then, you check if its NOT equal to nil, meaning if its anything but nil,

an else statement will just act as a “last resort”, basically "if its not nil, do this, but if it is, print the error:

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant ~= nil then
		local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
		local printmsg = plr:WaitForChild("PlayerGui")
		local localcontroller = script.Controller
		local controllerclone1 = localcontroller:Clone()
		controllerclone1.Parent = printmsg
	else	
		controllerclone1:Destroy()
	end
end)

This code does the exact same, just less lines. There is a difference between “overcomplicating” things, and then just general good practice of code and keeping things neat, in this case, its good practice to use the correct methods.