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

What is wrong with this script? [Unanswered]

Asked by 10 years ago

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
0
What does Output print when you run the script? IcyArticunoX 355 — 10y
0
It doesn't say anything, the game thinks that it's perfectly fine. SchonATL 15 — 10y

2 answers

Log in to vote
0
Answered by 10 years ago

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!

0
Not working :/ SchonATL 15 — 10y
0
In Studio, go to Test> Start Server and two screen (maybe a third for the Firewall), one will be the Server and one will be the client. Do what you have to in the client. When you leave the client, look in the output in the server. Post any errors please. TheStudentPilot 75 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

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

Answer this question