Adding on to if statement causes it to act differently

  1. What do you want to achieve?
    I’m making a combat system, and I just made a stun system, I’m trying to not allow the player to attack if he’s stunned

  2. What is the issue?
    If I check if the player isnt idle and return it works fine, but if I add if player isnt idle and they’re stunned it causes the attack event to fire multiple times when it shouldn’t

server script

attackevent.OnServerEvent:Connect(function(player, attacklist, localstate)
	local character = player.Character
	local torso = character.HumanoidRootPart

	local playerstate = character:GetAttribute("State")
	
	if playerstate ~= "Idle" and character:GetAttribute("Stun") == true then return end
	
	for i,v in attacklist do
		local opponent = v
		local opponentstate = opponent:GetAttribute("State")
		local opponenttorso = opponent.HumanoidRootPart
		
		if opponentstate == "Evade" then continue end
		
		if opponent.Name ~= "Rig" then
			
			if player.Team == players:GetPlayerFromCharacter(opponent).Team then print("same team") continue end
			
			if (opponenttorso.Position - torso.Position).Magnitude <= 12 then
				
				opponent.Humanoid:TakeDamage(10)
			end
			
		else
			opponent.Humanoid:TakeDamage(10)
		end
	end
	
	character:SetAttribute("State", "Attack")
	task.wait(.5)
	character:SetAttribute("State", "Idle")
end)

it works if I check on a seperate line but not on the same, not sure why

Please never cook like that

Why not make Stun as a playerstate?

if playerstate == "Stun"  then return end

would make a bit more sense.

I made the stun not a player state so it could be easier, because I also have a stuntimer attribute, and checking whenever player state is stun instead of having it separate is easier. Also that doesn’t answer my question?

Rewrite whole logic then.
You seem to not understand how operator “and” works Operators | Documentation - Roblox Creator Hub

well, I don’t really have to rewrite it, as I said it works if I check on a separate line. The question I asked was why it doesn’t work if I check on the same, the state is idle and stun is false both times. Also the API doesn’t have an example like I was trying to do.

I was asking why it doesn’t work if you use a ~= instead and check on the same line, I wasn’t asking to rewrite my code

Why would you check for player state anyway?You fire this event during the attack, so you should be just checking if the player is stunned or not.

What’s wrong with the loop?

Also, @NarutoUzmakiAkatsuki can you provide the script containing :FireServer? I think the issue might be occuring because you send attack queries too often and the return conditional can pass more often if character:GetAttribute("Stun") == true is false.

That part specifically is uhhhhh
image

Oh I guess yeah memory reservation brain damage

They should’ve done something like for i, opponent in attacklist do if their main goal was readability while not wasting memory ngl

It does feel like a trivial optimization though but yeah it’s something that can be done so yeah it should be done haha

I’m checking for the player state because attributes don’t replicate from the client and I’m changing it in the server, if I don’t check then the player could attack again or they should not be able too. Handling the player states on the client isn’t a good idea. Also the player can exploit and change their client which I’d like to check for.

Are you making sure that the “Stun” attribute actually exists and that its value is either true or false?

yes, stun attribute exists. I think I found out, that when doing an if statement with strings is a little bit different that using it with bools, which I think is the issue

But is it either true or false? It is a Boolean attribute, right?

yes the stun attributes aren’t the problem. I’m pretty sure it’s because how the if statements, and “and” works.

What is supposed to be the problem then? Do you have trouble with the AND operator?

I was just confused on why it would do damage when it shouldn’t, not what I was trying to do. I have seen people use “and” for checking if a player can’t do something, but I think those were bool values and not strings

It shouldn’t work any differently if you’re using strings or booleans or anything.

did you see the images I posted above?

using bools


using strings

First of all, those images were not the same as the one you posted above.

And the reason you’re getting dinner even when there’s no pasta is false is bc your logic is wrong. You should be using the OR operator.

If pasta ~= “pasta” or tomatosauce == false then return end

but those are literally copy linked images from the 3 images I posted in this topic.

I know about using or, I just said I was confused about why it wasn’t printing using ~=

also the reason I didn’t use “or” in the original topic post was because if I remember correctly or doesn’t take more than two arguments, and for combat systems I have seen scripts do long checks using and. Which is why I was confused