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

I made a AFK button for my minigames but it does not work and does not return any errors?

Asked by 7 years ago

So I have a script here:

Button = script.Parent.TextButton

player = script.Parent.Parent.Parent

ready = player.Ready

Button.MouseButton1Click:connect(function()
    if ready.Value == 1 then
        ready.Value = 0
        Button.Text = "AFK"
    end
    if ready.Value == 0 then
        ready.Value = 1
        Button.Text = "Active"
    end
end)

I checked everything and all the values like button player and ready are correctly identifying their objects. (the player value is the parent of started GUI). When I run the script and try to trigger the text button, nothing happens and there is no error in the output.. What is the solution?

0
Show me the script where the they TP or do something with the players. It can contain there a fault. Difined_Person 61 — 7y

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You could try indexing the LocalPlayer in a LocalScript.

Button = script.Parent.TextButton

player = game.Players.LocalPlayer

ready = player.Ready

Button.MouseButton1Click:connect(function()
    if ready.Value == 1 then
        ready.Value = 0
        Button.Text = "AFK"
    end
    if ready.Value == 0 then
        ready.Value = 1
        Button.Text = "Active"
    end
end)

Apply some logic operators to shorten your code;)

local player = game.Players.LocalPlayer;
local ready = player.Ready;
local Button = script.Parent.TextButton;

Button.MouseButton1Click:connect(function()
    ready.Value = (ready.Value == 1 and 0) or 1;
    Button.Text = (ready.Value == 1 and "AFK") or "Active";
end)
Ad
Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Belive me or not, but this is a pretty common mistake in programming.

Button = script.Parent.TextButton

player = script.Parent.Parent.Parent

ready = player.Ready

Button.MouseButton1Click:connect(function()
    -- ready.Value equals 1
    if ready.Value == 1 then --true
        ready.Value = 0 -- switch
        Button.Text = "AFK"
    end
    -- but now it equals 0!
    if ready.Value == 0 then--true
        ready.Value = 1 --switch
        Button.Text = "Active"
    -- in the end, it equals 1
    end
end)

Because you don't end the function after the first switch, the next code block is executed, and the second switch happens.

Answer this question