I’m making it so that if a player jumps they will be teleported out of a box they’re in, I then disconnect the remote event that makes that happen. It works but I get an error as well.
local jumpConnection
jumpConnection = JumpEvent.OnServerEvent:Connect(function(bool)
if bool then
jumpConnection:Disconnect()
char.JumpDetector:Destroy()
leave(player)
end
end)
This is the error I get,
Workspace.TeleBox3.BoxScript:97: attempt to index nil with ‘Disconnect’
The error only occurs after the first time, so I leave the box without an error the first time but after I enter again I get the error.
Is there a reason you’re destroying the JumpDetector? And, if so, is there anywhere in the script where you add that JumpDetector back?
From what I can tell when you’re destroying the JumpDetector, it’s no longer in the character and therefore the script can’t find it, causing it to index nil with “:Destroy()”
The JumpDetector is a local script I insert into the character to detect when the player jumps. I add it back when they enter again. I think the problem is with the variable jumpConnection, it says that is nil? So it thinks that jumpConnection doesn’t exist?
Yes, that’s what the problem is. My bad, I read the error incorrectly.
I’m wondering if the bool doesn’t show up the first time, but then after that it does. For testing purposes, try printing “bool found” after the if statement:
local jumpConnection
jumpConnection = JumpEvent.OnServerEvent:Connect(function(bool)
if bool then
print("bool found")
jumpConnection:Disconnect()
char.JumpDetector:Destroy()
leave(player)
end
end)
If it does print “bool found” the first time, then that ISN’T the route of the problem.
I tried doing that and it prints it every time. Nothing goes wrong in the actual game, the only thing that happens is the error message pops up. Would there be anything wrong with not trying to fix it if doesn’t actually break anything?
You’re right actually, there’s not any reason to disconnect it. It’s not going to be receiving anything anyways because I destroyed the jump detector so theres no reason to keep the variable.
I just rewrote it and it works just as before but now without errors, thanks!