Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

1 comment

0

Show me the script where the they TP or do something with the players. It can contain there a fault.

2 answers

✓ Accepted answer

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)

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.