If Statement running, despite there being Children inside of an Object? [SOLVED]

Hello there. On the second touch of the whiteboard, if there are no children, I want to change the board to black. Howbeit, if there are children on the second touch, I want the colour of the frame to stay white, but I am having some problems with it.

This is where the problem is:
local ON = Color3.fromRGB(255, 255, 255) --white screen
local OFF = Color3.fromRGB(47, 47, 47) --black screen

Whiteboard.TouchEnded:Connect(function()
	if copy.Enabled == false then --this only applies if it isn't enabled
		if #BoardScreen.Container:GetChildren() == 0 then --if there are no children then it turns off
			BoardScreen.BackgroundColor3 = OFF
			task.wait(1)
			perms = false
		elseif #BoardScreen.Container:GetChildren() ~= 0 then --if there are children it stays as white
			BoardScreen.BackgroundColor3 = ON
			task.wait(1)
			perms = false
		end
	elseif copy.Enabled == true then
		BoardScreen.BackgroundColor3 = ON
		ScreenLabel.TextTransparency = 0
		task.wait(2)
		ScreenLabel.TextTransparency = 1
		perms = true
	end
end)

I have checked the path, and the path seems to be fine, with no errors in the output.

This is the full Touch Script, which has been updated:

local perms = false 
local debounce = false
		
local Whiteboard = script.Parent --part
local BoardScreen = Whiteboard.SurfaceGui.Frame --frame
local ScreenLabel = BoardScreen.ScreenLabel --surfacegui
		
Whiteboard.Touched:Connect(function(marker)
	if marker ~= nil and marker.Parent ~= nil and marker.Parent:FindFirstChild("CardNumber") ~= nil and marker.Parent.CardNumber.Value == 348 then
		if not debounce then
			debounce = true
			if perms then
				copy.Enabled = false
				copy.ImageButton.LocalScript.Disabled = false
			else
				copy.Enabled = true
				copy.ImageButton.LocalScript.Disabled = false
			end
		end
		debounce = false
	end
end)
		
local ON = Color3.fromRGB(255, 255, 255)
local OFF = Color3.fromRGB(47, 47, 47)

Whiteboard.TouchEnded:Connect(function()
	if copy.Enabled == false then
		if #BoardScreen.Container:GetChildren() == 0 then
			BoardScreen.BackgroundColor3 = OFF
			task.wait(1)
			perms = false
		elseif #BoardScreen.Container:GetChildren() ~= 0 then
			BoardScreen.BackgroundColor3 = ON
			task.wait(1)
			perms = false
		end
	elseif copy.Enabled == true then
		BoardScreen.BackgroundColor3 = ON
		ScreenLabel.TextTransparency = 0
		task.wait(2)
		ScreenLabel.TextTransparency = 1
		perms = true
	end
end)

I have tried changing where I put the if statement, but so far nothing has worked.

These are the questions that I have:

  1. Am I doing something wrong?
  2. Is there a better, alternative script that would work better?
This is a reference to the last topic:

Wait() for every time the Touch Functions are Called; not working properly [SOLVED]

Any ideas or replies are appreciated.

1 Like

if you’re adding the children on clientside, the serverside script won’t see them

2 Likes

The children are added through a LocalScript in PlayerGui, but the children I am checking via a Script in Workspace, are in Workspace.

1 Like

If you’re adding the children clientside (via local script) and listening for childrenadded on the server (via a normal script), then it’s not gonna detect the children added.

You can add the children via server, or add some sorts of event to replicate the children from client to server, but making sure there is sanity checks and protection against exploit.

3 Likes
  1. What might this look like?

  2. Which option, do you think might work the best? If not, could I maybe fire from the Server somehow, and have the children checked in a LocalScript. If that is where it would work better maybe? RemoteEvent or Binable?

  • The problem is, is that I want to use .Touch which will only work in a normal Script, but then I need a LocalScript for the children. That is for it to work.

@aruruy0155, the children are currently inside of a Frame, called (Container).

Any ideas?

.Touched works on local scripts
you just can’t put the local script inside of the part

an alternative would be putting it in StarterPlayerScripts like:

workspace.Baseplate.Touched:Connect(print)
1 Like

If I can’t check the children though a ServerScript, would I just put it in a LocalScript and have two scripts instead of one; maybe?

yes, you’ll need to check the children in a local script
but what do you mean by “and have two scripts instead of one”?

1 Like

Have one Script for the .Touch event, and the LocalScript to check the children there instead, by using a RemoteEvent?

yes, that would work too if you’re updating the server with how many children there are whenever the number of children change

1 Like

Would I have to update the ServerScript, for it to work?

assuming you want the serverscript to know how many children there are, you’d have something like this

--server script
numChildren = 0
RemoteEvent.OnServerEvent:Connect(function(player, numChildrenByPlayer)
   validate player
   numChildren = numChildrenByPlayer
end)

-- local script
function tell server how many children there are
   Remote:FireServer(numChildren)
end

on child added to frame
   tell server how many children there are

on child removed from frame 
   tell server how many children there are

How would I fire a RemoteEvent from the Server if that is possible, and listen for it in a LocalScript, for the if #BoardScreen.Container:GetChildren() == 0 then, to happen?

If not, is there an alternative?

you can fire from the server like

--server script
someRemote:FireClient(somePlayer, 2, 4, 8)


--local script
function someFunction(a, b, c)
   print(a, b, c) --prints 2, 4, and 8
end

someRemote.OnClientEvent:Connect(someFunction)
2 Likes

Should I put the Script and the LocalScript next to each other, or have the RemoteEvent under the ServerScript, and the LocalScript to be under that?

they can be anywhere as long as the local script is somewhere local scripts run, the server script is somewhere server scripts run, both scripts can see the remote event, and the local script is setup to reference the new gui if starterGui is recreating the gui whenever you respawn

1 Like

Says “Argument 1 missing or nil”, in the Output:

Whiteboard.TouchEnded:Connect(function()
			if copy.Enabled == false then
				local RemoteEvent = script.Parent.RemoteEvent
				RemoteEvent:FireClient() --here is the error
				task.wait(1)
				perms = false
			elseif copy.Enabled == true then
				BoardScreen.BackgroundColor3 = ON
				ScreenLabel.TextTransparency = 0
				task.wait(2)
				ScreenLabel.TextTransparency = 1
				perms = true
			end
		end) 

I think I may need to put something in the brackets ‘()’, but not sure.

you need to specify the player to fire to
I think it would either be all players, so you’d instead of :FireAllClients()
or you’d fire to the player that touched it, so you’d work with the argument given by .TouchEnded to figure out which player it is

1 Like
if marker ~= nil and marker.Parent ~= nil and marker.Parent:FindFirstChild("CardNumber") ~= nil and marker.Parent.CardNumber.Value == 348 then

Basically, I want it to be anybody who has the right tool and the correct number in the Card, but how would I put that inside the brackets (if I need to)?

what make a tool the right tool?
what’s the correct number card?
and what parts of this are clientside? what parts of this are serverside?