Need help on if statement

It’s basically an if statement “if Player.Backpack:FindFirstChild(“Card”)” and if the surface gui’s text on the card is the same number as “t” the number then it will run the function. (the player is not only the client but the whole server’s player) Sorry if this is quite confusing.

1 Like

This is just an example, modify it to work on your game

if Player.Backpack:FindFirstChild(“Card”) and Player.Backpack.Card.SurfaceGui.Text == t then

end
1 Like
local card = Player.Backpack:FindFirstChild("Card")
if card then--assert the card exists in backpack first
    local num = tonumber(--find the surfaceGui's Text where the number is)
    if num then--assert num *is* a number, else the code should stop
        if num == t then
            --call your function
        end
    end
end

This is a safer method handling the possibility the card and/or the number on it do not exist.
If you’re adapting this to checking all players, hold a boolean value prior to looping through them, initialized to true. It’ll represent whether everyone is OK or not. If you ever encounter a players’ card s.t. num ~= t, set the bool to false and break.
Check after if the bool is true/false to see if everyones’ card is equivalent or not.

2 Likes

I am confused on how it works. Would you mind explaining it again?

1 Like

Sure. This line:
local card = Player.Backpack:FindFirstChild("Card")
will store the reference to the “Card” instance inside the variable card, if it exists. If it doesn’t, it will return nil. Thus,
if card then
is stating “if card exists, then…” should card not exist, “if nil exists” is false, so the embedded code won’t run.

Same idea for num – tonumber([the text label].Text) will convert a string into a number, if it can be converted to a number. This will return nil if .Text contains something that’s not numerical, like “hello.”
That means if num then is saying “if num contains a valid number, then…”

Finally, if num == t then is saying "If the number held in num and the number held in t are the same, then…``

You can provide alternative code to these statement by using else-statements, but they don’t seem necessary by your code requirements.

2 Likes

Problem: you are using FindFirstChild and not using it

1 Like

I’m using FindFirstChild to check if Card exists in the backpack. If it doesn’t exist, then the if statement will not continue, but if it exists, then it will check for the next argument which checks if the text is equal to t.

It’s still bad practice. you should assign a variable for it.

1 Like

Hello, is this script correct?

Control = script.Parent.Control

ABlack2 = script.Parent.ABlack2

-------------------------
t = 1000
Counter1 = script.Parent.Parent.TV.Room1


Button1 = script.Parent.RealScreen1.Screen.Normal.Next

-------------------------

script.Parent.ABlack2.ClickDetector.MouseClick:Connect(function(Player)
	if Player.Team.Name == "Medical Staff" then
		local card = Player.Backpack:FindFirstChild("Card")
		if card then
		local num = tonumber(card.Handle.SurfaceGui.TextBox.Text)
		if num then
		if num == t then
		t += 1
		Counter1.SurfaceGui.TextBox.Text = t
		script.Parent.Parent.TV.Room1.SurfaceGui.TextBox.Text = t 
		Button1.Text = t 
		Control.Dong.Playing = true
		wait(1)
		Control.Dong.Playing = false
				end
			end
		end
	end
end)

(I tested it, doesn’t work for some reason.)

I added a bunch of debug statements. Let me know what it outputs? It’ll tell you on each operation if the card is good or not.

script.Parent.ABlack2.ClickDetector.MouseClick:Connect(function(Player)
	if Player.Team.Name == "Medical Staff" then
		print(Player.Name .. ' is medical staff...')
		local card = Player.Backpack:FindFirstChild("Card")
		if card then
			print(Player.Name .. ' has a card...')
			local num = tonumber(card.Handle.SurfaceGui.TextBox.Text)
			if num then
				print(string.format("%s's card is number #%d", Player.Name, num))
				if num == t then
					print(string.format("%s's card is the correct number (%d == %d)", Player.Name, num, t))
					t += 1
					
					Counter1.SurfaceGui.TextBox.Text = t
					script.Parent.Parent.TV.Room1.SurfaceGui.TextBox.Text = t 
					Button1.Text = t 
					
					Control.Dong:Play()
				else
					warn(string.format("%s's card isn't the right number (%d =/= %d"), Player.Name, num, t))
				end
			else
				warn(Player.Name .. '\'s card does not have a number')
			end
		else
			warn(Player.Name .. ' does not have a card')
		end
	else
		warn(Player.Name .. ' is not Medical Staff')
	end
end)

Is there an error? Run it and see what text appears in the console from the print/warn statements
Edit Sorry just noticed a typo.
The two underlined strings – it should be “%s’s” not “%'s” I’ll edit my original snippet

Only this, it doesn’t print/warn the other statements. Workspace.C1PC.PC1.PC1 Module:52: Expected identifier when parsing expression, got ')'

Fixed the typo, there’s still an error. Workspace.C1PC.PC1.PC1 Module:52: Expected identifier when parsing expression, got ')'

Which line is 52? I’m not seeing a missing closing parenthesis in that snippet

This one. needed more words

Oh, move the “)” at the end of that string inside the quote so it reads “%d)”

Like this?

					warn(string.format("%s's card isn't the right number (%d =/= %d)"), Player.Name, num, t)

Almost, it was the other ). Should read:
warn(string.format("%s's card isn't the right number (%d =/= %d)", Player.Name, num, t))
My bad for the errors I wasn’t able to compile the code without replicating the rest of your system

1 Like