Variable set to true but the check still passes through it

I have a check where if a certain key is pressed and the variable enabled is equal to false then things can start happening, but it just goes right through the check. Any ideas what the issue might be?

Web.OnServerEvent:Connect(function(plr, hit, letter)
	print(hit)
	local LeftHand = plr.Character:WaitForChild("Left Arm")
	local RightHand = plr.Character:WaitForChild("Right Arm")
	local Enabled = false
	if letter == 1 and not Enabled then
		Enabled = true
		print("This is the left hand and ".. plr.Name.. " pressed Q.")
		print(letter)
	end
	if letter == 2 and not Enabled then
		Enabled = true
		print("This is the right hand and ".. plr.Name.. " pressed E.")
		print(letter)
	end
end)
1 Like

Have you tried setting it to false afterwards

I’m trying to make it so it doesnt pass through the if statement when its not enabled, if I make it false at the end of the if statements then it’ll just keep passing through the ifs.

You should just use elseif
Than just two if statements

1 Like

I like having the two if statements because, I have trouble keeping track of ends and combining it doesn’t work well for my brain, anyway I don’t think that will fix my problem

What does the output print

30char

It doesnt print any errors, it just prints what I have in the if statements.
Like I said it’s just passing through the IF statements when I have it set to enabled

Not related to your problem, but just wanted to tell you that this might run poorly when the game is published. Things will happen too late due to replication. When you press the key from the client, and you send a signal to the server telling it to do stuff, that sending will take some time which is why things happen late. The overhead might not be noticeable in studio.

Do you necessarily need to do this from the server?

1 Like

If you set it to true what does the Enable print False or True

Well I need it to be server-sided also I don’t think it matters in this case since I’m just making this for fun and practice

1 Like

How would I eliminate that without having to do everything on client, I want other players to see, the webs shooting out of other players hands

The Enabled variable is being created locally to that event, you might want to create the variable outside of the event function

such as

local Enabled = false <-- set it here, outside of the function
Web.OnServerEvent:Connect(function(plr, hit, letter)
... logic here
end)

if the variable is created inside of the event then a new Enabled variable gets created everytime that event is fired.

2 Likes

ohhhhhhhhhhhhhhhhhhhhhhhhhhhhh, I remember having this problem before

Your awesome thanks man :call_me_hand: :call_me_hand:

Don’t do

Enabled = true

do

local Enabled = true

I think this was the problem in your script.

Huh, what are you talking about?

I already initialized the variable…

Nevermind, I see it has already been solved.

No, they are trying to set the variable from False to True, they do not want to create a new variable inside of the if

remember,

local A = value  --creates a new variable
A = new_value --sets the already created variable

It’s working very great thank you again!