Define player from server script in TextButton

I am trying to have a BoolValue inside the player toggle between true and false when the player left clicks the TextButton.

I have this script in SSS to grant the BoolValue to the player:

local Players = game:GetService("Players")
local function onPlayerAdded(Player)
	local Select=Instance.new("BoolValue")
	Select.Parent=Player
	Select.Name="Select"
end
Players.PlayerAdded:Connect(onPlayerAdded)

And this is the script inside the Text Button:

Button=script.Parent
Button.MouseButton1Click:Connect(function(plr)
	local Select=plr:WaitForChild("Select")
	if Select.Value==true then
		Select.Value=false
	end
	if Select.Value==false then
		Select.Value=true
	end
end)

However, when I ran this i got the error:
"attempt to index nil with “WaitForChild”
Thanks for any help! :slight_smile:

This should work.

Server Script:

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

Local Script:

local Button = script.Parent
Button.MouseButton1Click:Connect(function(player)
	local Select = player:FindFirstChild("Select")
	if Select.Value == true then
		Select.Value = false
	end

	if Select.Value == false then
		Select.Value = true
	end
end)

Thanks for the response, but I was trying to do this with a server script, as I want the BoolValue to update on the serverside for use in other server scripts. Is this possible? or will I have to find another way to replicate the change from the client to the server?
Again, thanks.

I don’t really understand what you’re trying to accomplish here. From what you said, you were making a “BoolValue inside the player toggle between true and false when the player left clicks the TextButton.”

The BoolValue gets added onto the player in the Server Script, and then the Local Script detects if its value is either true or false and sets it to the opposite.

Use a remote event to change the bool value so that the server can detect if it has changed

1 Like

I think if I change the value of the BoolValue using a local script as you suggest, those changes won’t happen on the server, but only the client right?

I get what you mean now. If you want the value to change to the whole server, you would need a remote event.

Server Script:

local BoolRemoteEvent = game.ReplicatedStorage.BoolRE --reference your RemoteEvent here

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

BoolRemoteEvent.OnServerEvent:Connect(function(player)
local Select = player:FindFirstChild("Select")

	if Select.Value == true then
		Select.Value = false
	end

	if Select.Value == false then
		Select.Value = true
	end
end)

Local Script:

local BoolRemoteEvent = game.ReplicatedStorage.BoolRE --reference your RemoteEvent here

local Button = script.Parent
Button.MouseButton1Click:Connect(function(player)
	BoolRemoteEvent:FireServer()
end)

This worked, just had to alter it slightly because the server script would instantly change the value back to “true”. Thanks :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.