I have a tycoon game where I am trying to get it set up so that when a player leaves the game, the old tycoon is deleted and it is replaced with a brand new one so that a new player can join the game and purchase that tycoon. However, when I run the game I get an error in the output that states:
21: attempt to index nil with 'PlayerRemoving'
Here is my code:
currentPlayer = game:GetService("Players").LocalPlayer factory = script.Parent.Factory owner = script.Parent.Factory.ownerName local clone = factory:Clone() function regenFactory() --Function to destroy the factory and regenerate a new one in it's place. factory:Destroy() wait(0.5) factory = clone:Clone() factory.Parent = script.Parent owner = script.Parent.Factory.ownerName end currentPlayer.PlayerRemoving:Connect(function(player) --Function that tells the game when a player leaves, and if they leave, run the regenFactory function. regenFactory() end)
Any help would be appreciated, thank you!