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)
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.
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
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.
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’
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?
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
--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)
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.
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
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.
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: