Theres no output I'm trying to get the player removing to work this is a tycoon I want it so if that player leaves it resets the tycoon Ive asked this several times I need help
function onTouch(hit) local name = hit.Parent.Name local player = hit.Parent local plr = hit.Parent:FindFirstChild("Humanoid") if plr then local check1 = game.Players:GetPlayerFromCharacter(hit.Parent) if check1 then local check2 = check1:WaitForChild("leaderstats") if check2 then local check3 = check2:WaitForChild("Owner") if check3.Value == false then check3.Value = true script.Parent.CanCollide = false script.Parent.Transparency = 1 print(hit.Parent) local model = script.Parent.Parent model.Name = name.." Owns This Tycoon" local green = game.Teams.GreenTeam check1.TeamColor = BrickColor.new("Lime green") local h = player:FindFirstChild("Humanoid") if h then h.Health = 0 game.Players.PlayerRemoving:connect(function(player) if player == false then check3.Value = false model.Name = "Become Owner" script.Parent.CanCollide = false script.Parent.Transparency = 1 end end) end end end end end end script.Parent.Touched:connect(onTouch)
First of all your Tycoon script has a major design flaw. Instead of having a string Value (an Object that holds a text value) inside of the tycoon model to keep track of the name of the player who owns the tycoon you instead keep a boolean Value (an Object that holds a true or false value) on the player, which tracks whether a player owns ANY tycoon or none at all.
This means whenever a player touches the brick, it sets their own boolean value to true and changes the name of the tycoon's model to "<PlayerName> Owns this Tycoon". But the game never keeps track of who owns it, only that they own one, so anyone who touches the brick will now see their name displayed above it, even if a player has previously claimed this tycoon.
This being said, let's now take a look at the true subject of your question.
The code inside the PlayerRemoving callback is never being executed because the if player == false then
statement always fail. PlayerRemoving always pass the player's game.Players
instance as first argument so you can know who left the game.
So let's say I were to leave the game, your callback -- The function you connect()
'ed to the PlayerRemoving event -- would be given the object game.Players.Link150
. But here you are testing whether "player" is false, which isn't: It is equal to game.Players.Link150
.
Hope that helped you. Be sure to upvote and accept my answer if it did. If you need anything else, leave me a message.
game.Players.PlayerRemoving:connect(function(player) local PlayerThatLeft = player.Name local Area = game.workspace:GetChildren() for i, v in ipairs(Area) do if v.Name = PlayerThatLeft then v. -- Do whatever to reset the tycoon end end end end)
On Line 7 what it does is look at everything in the workspace to find something with the same name as the player who is leaving, If your tycoon changes the name of something to the player who is using it it will find that, then you do whatever
For example lets say that a part with the player name is in the tycoon and you want to edit another parts color when they leave We'll name that part 'Dookie'(lel)
so you would change line 8 to
v.Parent.Dookie.BrickColor = BrickColor.new('Whatever')
I saw in your script you wanted to access the player
game.Players.PlayerRemoving:connect(function(player) Leaderstats = player:WaitForChild('leaderstats') Owner = Leaderstats:WaitForChild('Owner') end)
Hope this has helped