It's supposed to check every tenth of a second to see if the 'owner' has left the game, and if he/she has then reset the tycoon.
while true do wait(0.1) game.Players.PlayerRemoving:connect(function(player) if player.Name==script.Parent.Owner.Value then script.Parent.Owner.Value = "Nobody" ------------------------------------ local gate = script.Parent.Gate if gate.Transparency ~= 0 then gate.Transparency = 0 gate.CanCollide = true end ------------------------------------ local co = script.Parent.OwnershipDoor.ClaimOwnership co.BrickColor = BrickColor.new("Medium stone grey") co.Parent.OwnerName.SurfaceGui.Owner.Text = "Claim Ownership" co.Defender.Disabled = true co.Claim.Disabled = false ------------------------------------ end end) end
You don't need to run a while loop for a function. A function is called upon what you said... in this case, when a player leaves.
Look at it like this, I join a game, finish my tycoon and decide to leave. Your code looks as follows:
game.Players.PlayerAdded:connect(function(player) -- code here end)
So, I hit the X in the corner of the screen. All of your code inside of that first function is run, and I am now known as 'player'. So now you can see if me (player) has reset my tycoon and any other stuff.
Side Note
Instead of doing a seperate wait()
inside of a while
loop, you can write the code as while wait() do
.
For all of those BrickColor.new("Medium stone grey")
's you can easily do BrickColor.Gray()
. To check if the color will work for you (.Gray()
does Medium stone grey), you can run this in the command line: print(BrickColor.Gray())
. Use this Wiki page to learn more about BrickColors.
Anything else, let me know!
If it's a LocalScript, then PlayerAdded and PlayerRemoving doesn't work. Instead, use ChildAdded and ChildRemoved.
game.Players.ChildRemoved:connect(function(child) if child.ClassName == 'Player' then --Code goes here end end)
Here's using the above in your script:
while true do wait(0.1) game.Players.ChildRemoved:connect(function(child) if child.ClassName == 'Player' then local player = child if player.Name==script.Parent.Owner.Value then script.Parent.Owner.Value = "Nobody" ------------------------------------ local gate = script.Parent.Gate if gate.Transparency ~= 0 then gate.Transparency = 0 gate.CanCollide = true end ------------------------------------ local co = script.Parent.OwnershipDoor.ClaimOwnership co.BrickColor = BrickColor.new("Medium stone grey") co.Parent.OwnerName.SurfaceGui.Owner.Text = "Claim Ownership" co.Defender.Disabled = true co.Claim.Disabled = false ------------------------------------ end end end) end