Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Unable to regen tycoon for player who leaves?

Asked by
Asalea 25
1 year ago

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!

0
PlayerRemoving it's a game.Players event, not from the localPlayer santi_luly1 47 — 1y

Answer this question