Value resets somehow

Hi, I want to create a button to toggle afk/ready (Like in epic minigames)
I used a BoolValue for this.
Here’s the code for the button

script.Parent.MouseButton1Click:Connect(function()
	local val = game.Players.LocalPlayer.InGame.Value
	print("1")
	print(val)
	if val == true then
		script.Parent.BackgroundColor3 = Color3.new(0, 255, 0)
		script.Parent.Text = "ON"
		val = false
		print("2")
		print(val)
	else
		script.Parent.Text = "OFF"
		script.Parent.BackgroundColor3 = Color3.new(255, 0, 0)
		val = true
		print("3")
		print(val)
	end
end)

When the game starts, the Value is true. If you click the button, it changes to false. But then somehow it resets back to true again because when I clicked the button again the value was true instead of false.

About the BoolValue:

game.Players.PlayerAdded:Connect(function(player)
	local value = Instance.new("BoolValue")
	value.Name = "InGame"
	value.Value = true
	value.Parent = player
end)

2 Likes

Hello! This could be because you are setting the value to false and then checking if it is false in the else statement but I am not entirely sure.

1 Like

From reading you code, I don’t see where or how the value is being set to true. Your two conditions can’t satisfy at the same time, because your value can never be true and false. It doesn’t seem like the value is reset, but actually you’re are reading it incorrectly. Hmm, try moving the variable statement outside of the MouseButton1Click function.

2 Likes

Another thing I think that would help would be when you define the variable do not do it directly. Since the player doesn’t actually exist unless you are in game you want to use this:

local val = game.Players.LocalPlayer:FindFirstChild("InGame").Value

Hope this helps!

2 Likes

The first code was in a local script. I now put a normal script into the button too.
local script

script.Parent.MouseButton1Click:Connect(function()
	local val = game.Players.LocalPlayer.InGame.Value
	if val == true then
		script.Parent.BackgroundColor3 = Color3.new(0, 255, 0)
		script.Parent.Text = "ON"
	else
		script.Parent.Text = "OFF"
		script.Parent.BackgroundColor3 = Color3.new(255, 0, 0)
	end
end)

script

script.Parent.MouseButton1Click:Connect(function(click)
	local val = click.InGame.Value
	if val == true then
		val = false
	else
		val = true
	end
end)

The code in the local script works, But the normal script gives me this output:
"Players.JayPlays_JP.PlayerGui.Settings.SettingsFrame.AFKButton.AFKvalue:2: attempt to index nil with ‘InGame’ "

I checked game.Players.(MyUsername).InGame and it’s there.

1 Like

Players.JayPlays_JP.PlayerGui.Settings.SettingsFrame.AFKButton.AFKvalue:2: attempt to index nil with ‘FindFirstChild’
Same output. But instead of ‘InGame’ It now says ‘FindFirstChild’

1 Like

If the normal script is in the GUI then it won’t work because all GUI are client sided, so you need to use a local script.

1 Like

But I want to change the value inside of the player. So I can access it from different scripts. If I change the value from a local script, wouldn’t the value only be different on the client? Or is there another way to change values from localscripts?

1 Like

Nope if you change it from a local script it will be available everywhere else.

1 Like

remote event? Ok i gotta check yt because I have no Idea what this is.

1 Like

The problem is saving the variable

local val = game.Players.LocalPlayer.InGame.Value

this saves the value not the path.
When you do val = true or false later all you are doing is changing the value in the variable.

Instance.new("IntValue").Parent = script.Parent
print(script.Parent.IntValue.Value) -- prints 0
local Variable = script.Parent.IntValue.Value -- saves the int value 0
script.Parent.IntValue.Value = 5
print(Variable) -- prints 0
print(script.Parent.IntValue.Value) -- prints 5

You really need to do this in order for the value to be changed by the script

local val = game.Players.LocalPlayer.InGame
val.Value = true
val.Value = false
1 Like

You would simply do

--Server script inside ServerScriptService
game.ReplicatedStorage.RemoteEvent:Connect(function(player, value)
    player.InGame.Value = value
end)

and on this button you will just replace
val = false to game.ReplicatedStorage.RemoteEvent:FireServer(false)
(don’t forget to add a RemoteEvent inside ReplicatedStorage)

2 Likes

Oh yes! I didn’t see that. You want to define the variable and not include the

.Value
2 Likes

Incorrect, if you’re handling it locally, what the server reads would be different to what the client reads. In this case, as suggested above you would need to use remote events. Handle the Input on the client, and fire a remote to the server, where you implement the checks and etc. Since you’re changing the Gui properties after the checks, you might considering using RemoteFunctions to communicate between client and server, or you could just handle the Properties changes on server by referring to the player’s PlayerGui.

4 Likes

You want to define the variable without the .Value either way
Even on the server you could have these same issues, however I do agree that remote functions are probably necessary to resolve the issue. Although if the code is copy and pasted into the remote event there will be no better results

1 Like

This code works:

local val = game.Players.LocalPlayer.InGame

script.Parent.MouseButton1Click:Connect(function()
	if val.Value == true then
		script.Parent.BackgroundColor3 = Color3.new(0, 255, 0)
		script.Parent.Text = "ON"
		val.Value = false
	else
		script.Parent.Text = "OFF"
		script.Parent.BackgroundColor3 = Color3.new(255, 0, 0)
		val.Value = true
	end

end)
1 Like

It will work for the client side, if you print the value / reference it from server it will still be the value that the Server changed it to. For that , you need to use Remote Events, so you’re able to change it from the Server side.

1 Like

In order to create an afk button you need the code edits that you provided and a remote event.
A combination of both just said that here.

CC @WaterJamesPlough

2 Likes

Oh I see, I thought this was meant for client my mistake, sorry.

1 Like

Yes, use this code:

local val = game.Players.LocalPlayer.InGame

script.Parent.MouseButton1Click:Connect(function()
	if val.Value == true then
		script.Parent.BackgroundColor3 = Color3.new(0, 255, 0)
		script.Parent.Text = "ON"
		val.Value = false
	else
		script.Parent.Text = "OFF"
		script.Parent.BackgroundColor3 = Color3.new(255, 0, 0)
		val.Value = true
	end

end)

And use remote functions to pass on the value of InGame to the server, here is information about remote functions or events here is some information on both:

1 Like