Help with script not going to another If statement

Hello, I am creating a inventory system with 3 different modes that are switchable, but for some reason when I change them mid game it doesn’t work at all and it doesn’t go to the other If statements, when I start the game off with “Mode 2” it prints Mode 1? Any help


local Plr = game.Players.LocalPlayer
local player = game.Players.LocalPlayer
local character = player.Character
local selected = script.Parent.Parent.Frame.Handler.Selected
local LastEquippedTool = script.Parent.Parent.Frame.Handler.LastEquippedTool
local LastEquippedLight = script.Parent.Parent.Frame.Handler.LastEquippedLight

local IsEquipped = script.Parent.Equipped

local Mode = "Mode 1"

function UpdateMode()
	if script.Parent.Input.Value == "Mode 1" then
		Mode = "Mode 1"
	else
		if script.Parent.Input.Value == "Mode 2" then
			Mode = "Mode 2"
		else
			if script.Parent.Input.Value == "Mode 3" then
				Mode = "Mode 3"
			end
		end
	end
end

script.Parent.Input.Changed:Connect(UpdateMode)

Plr:GetMouse().KeyDown:Connect(function(E)
	if E == "f" then -- if right KeyBind
		
		if script.Parent.Parent.Delay.Value == false then
			script.Parent.Parent.Delay.Value = true
			
			if IsEquipped.Value == false then
				IsEquipped.Value = true
			
				if Mode == "Mode 1" then
					print("Unequip Anything In Hand")
					if script.Parent.Parent.Frame.Handler.Equipped.Value ~= nil then
						script.Parent.Parent.Frame.Frame.Equip.Text = "Equip"
						character.Humanoid:UnequipTools()
						script.Parent.Equipped.Value = false
						script.Parent.Parent.Frame.Handler.Equipped.Value = nil
	          else
					if Mode == "Mode 2" then
							print("Equip/Unequip Light")
						character.Humanoid:EquipTool(LastEquippedLight.Value)
						script.Parent.Parent.Frame.Frame.Visible = true
						script.Parent.Parent.Frame.Frame.Equip.Text = "Unequip"
						script.Parent.Parent.Frame.Handler.Selected.Value = LastEquippedLight.Value
						script.Parent.Parent.Frame.Frame.ImageLabel.Image = selected.Value.TextureId
						script.Parent.Parent.Frame.Frame.ItemName.Text = selected.Value.Name
						script.Parent.Parent.Frame.Handler.Location.Value = player
						script.Parent.Parent.Frame.Handler.Equipped.Value = selected.Value
				else
						if Mode == "Mode 3" then	
								print("Equip to Last Equiped Light, Unequip to Last Equipped Tool")
							if script.Parent.Parent.Frame.Handler.LastEquippedTool.Value ~= nil then
								character.Humanoid:EquipTool(LastEquippedTool.Value)
								script.Parent.Parent.Frame.Handler.Equipped.Value = LastEquippedTool.Value
								script.Parent.Parent.Frame.Handler.Selected.Value = LastEquippedTool.Value
								script.Parent.Parent.Frame.Frame.ItemName.Text = LastEquippedTool.Value.Name
								script.Parent.Parent.Frame.Frame.ImageLabel.Image = LastEquippedTool.Value.TextureId
								script.Parent.Parent.Frame.Handler.Location.Value = player
							elseif script.Parent.Parent.Frame.Handler.LastEquippedTool.Value == nil then
									character.Humanoid:UnequipTools()
								end
							end
					    end
					end
			    end
		    end
	    end
	end
end)
2 Likes

You might have to use elseif and not else… if but I could be really wrong

2 Likes

Just as a small amount of testing:
trying to change the .Changed thing to GetPropertyChangedSignal() (script.Parent.Input:GetPropertyChangedSignal("Value"):Connect(UpdateMode)), and add a print statement just after the start of the function

(i.e.

function UpdateMode()
    print("Input change detected")
	if script.Parent.Input.Value == "Mode 1" then

)

Oh alright let me get on and test it rn

No you’re absolutely correct. dont use else…if in the same statement when checking the same property or thing.

It prints it out when it changes but when I change it to Mode 2 and press f then it still prints out the print under “If Mode == “Mode 1” then” when it’s mode 2 not mode 1

It’s because I Dident do the Else’s right, I don’t know the difference between elseif and else if so I probably did it wrong

so replace the if’s thats under a else with Elseif?

1 Like

Well from what I can see, why do you have two variables assigned too the same child (Plr and player) No need for that.

local Character = game:GetService("Players").LocalPlayer.Character
local Selected = script.Parent.Parent.Frame.Handler.Selected --It recommended to name every individual child. So Frame would be inventory from what i'm assuming
local LastEquippedTool = script.Parent.Parent.Frame.Handler.LastEquippedTool
local LastEquippedLight = script.Parent.Parent.Frame.Handler.LastEquippedLight
local Equipped = script.Parent.Equipped

For the mode selection try using number variables instead of strings. A little simpler to deal with.

local Mode = 1

script.Parent.Input:GetPropertyChangedSignal("Value"):Connect(function()
   if script.Parent.Input.Value == 1 then --Replace the string with number
      Mode = 1
   elseif script.Parent.Input.Value == 2 then
      Mode = 2
   else
      Mode = 3
   end
end

Now for the last bit, try closing the if statements right after the function happens, (unless they’re supposed to be like that) and that should do the fix.

TIP: Make variables of the GUI’s and other child’s. for example:

local Handler = script.Parent.Parent.Frame.Handler.Equipped

It’ll save you quite a bit of time

local Plr = game.Players.LocalPlayer
local player = game.Players.LocalPlayer
local character = player.Character
local selected = script.Parent.Parent.Frame.Handler.Selected
local LastEquippedTool = script.Parent.Parent.Frame.Handler.LastEquippedTool
local LastEquippedLight = script.Parent.Parent.Frame.Handler.LastEquippedLight

local IsEquipped = script.Parent.Equipped

local Mode = "Mode 1"

function UpdateMode()
	if script.Parent.Input.Value == "Mode 1" then
		Mode = "Mode 1"
	elseif script.Parent.Input.Value == "Mode 2" then
		Mode = "Mode 2"
	elseif script.Parent.Input.Value == "Mode 3" then
		Mode = "Mode 3"
	end
end

script.Parent.Input.Changed:Connect(UpdateMode)

Plr:GetMouse().KeyDown:Connect(function(E)
	if E == "f" then -- if right KeyBind
		
		if script.Parent.Parent.Delay.Value == false then
			script.Parent.Parent.Delay.Value = true
			
			if IsEquipped.Value == false then
				IsEquipped.Value = true
			
				if Mode == "Mode 1" then
					print("Unequip Anything In Hand")
					if script.Parent.Parent.Frame.Handler.Equipped.Value ~= nil then
						script.Parent.Parent.Frame.Frame.Equip.Text = "Equip"
						character.Humanoid:UnequipTools()
						script.Parent.Equipped.Value = false
						script.Parent.Parent.Frame.Handler.Equipped.Value = nil
	          else
					if Mode == "Mode 2" then
							print("Equip/Unequip Light")
						character.Humanoid:EquipTool(LastEquippedLight.Value)
						script.Parent.Parent.Frame.Frame.Visible = true
						script.Parent.Parent.Frame.Frame.Equip.Text = "Unequip"
						script.Parent.Parent.Frame.Handler.Selected.Value = LastEquippedLight.Value
						script.Parent.Parent.Frame.Frame.ImageLabel.Image = selected.Value.TextureId
						script.Parent.Parent.Frame.Frame.ItemName.Text = selected.Value.Name
						script.Parent.Parent.Frame.Handler.Location.Value = player
						script.Parent.Parent.Frame.Handler.Equipped.Value = selected.Value
				else
						if Mode == "Mode 3" then	
								print("Equip to Last Equiped Light, Unequip to Last Equipped Tool")
							if script.Parent.Parent.Frame.Handler.LastEquippedTool.Value ~= nil then
								character.Humanoid:EquipTool(LastEquippedTool.Value)
								script.Parent.Parent.Frame.Handler.Equipped.Value = LastEquippedTool.Value
								script.Parent.Parent.Frame.Handler.Selected.Value = LastEquippedTool.Value
								script.Parent.Parent.Frame.Frame.ItemName.Text = LastEquippedTool.Value.Name
								script.Parent.Parent.Frame.Frame.ImageLabel.Image = LastEquippedTool.Value.TextureId
								script.Parent.Parent.Frame.Handler.Location.Value = player
							elseif script.Parent.Parent.Frame.Handler.LastEquippedTool.Value == nil then
									character.Humanoid:UnequipTools()
								end
							end
					    end
					end
			    end
		    end
	    end
	end
end)

May I ask, are you changing the value from a serverscript or a local script? (ik that the script provided is local, but where are you changing the value from)

Yeah pretty much, elseif behaves just like an if but it’s used for checking other specific conditions after the if.
Else just regards every other condition that doesn’t meet the if statements filter.

Directly from a local script.
Also since you mentioned that this is for the players inventory, this just cannot be done in a server script, by the server, or at least, it can’t be done in a practical way.

Make sure the value is inside StarterGui or inside the local script.

Hmm, in the line:

Plr:GetMouse().KeyDown:Connect(function(E)
   if E == "f" then

Are you trying to detect when a player presses the “F” or “E” key on the keyboard when the mouse is hovered over the ‘item’ in the inventory? If so try using the User Input Service.

(I’m not OP, so don’t keep on replying to me. Reply to the original post instead)

Oh my bad, Excuse Moi. :face_with_hand_over_mouth:

But it does get us closer to the solution.

i changed the Else to Elseif and the only one that prints is Mode 1, if i do mode 2 it doesn’t print anything but if i do mode 1 it prints, for some reason it doesnt get to mode 2 and 3

local Plr = game.Players.LocalPlayer
local player = game.Players.LocalPlayer
local character = player.Character
local selected = script.Parent.Parent.Frame.Handler.Selected
local LastEquippedTool = script.Parent.Parent.Frame.Handler.LastEquippedTool
local LastEquippedLight = script.Parent.Parent.Frame.Handler.LastEquippedLight

local IsEquipped = script.Parent.Equipped

Plr:GetMouse().KeyDown:Connect(function(E)
	if E == "f" then -- if right KeyBind
		
		if script.Parent.Parent.Delay.Value == false then
			script.Parent.Parent.Delay.Value = false
			
			if IsEquipped.Value == false then
			
				if script.Parent.Input.Value == "Mode 1" then
					print("Unequip Anything In Hand")
					if script.Parent.Parent.Frame.Handler.Equipped.Value ~= nil then
						script.Parent.Parent.Frame.Frame.Equip.Text = "Equip"
						character.Humanoid:UnequipTools()
						script.Parent.Equipped.Value = false
						script.Parent.Parent.Frame.Handler.Equipped.Value = nil
	          
						elseif script.Parent.Input.Value == "Mode 2" then
							print("Equip/Unequip Light")
						character.Humanoid:EquipTool(LastEquippedLight.Value)
						script.Parent.Parent.Frame.Frame.Visible = true
						script.Parent.Parent.Frame.Frame.Equip.Text = "Unequip"
						script.Parent.Parent.Frame.Handler.Selected.Value = LastEquippedLight.Value
						script.Parent.Parent.Frame.Frame.ImageLabel.Image = selected.Value.TextureId
						script.Parent.Parent.Frame.Frame.ItemName.Text = selected.Value.Name
						script.Parent.Parent.Frame.Handler.Location.Value = player
						script.Parent.Parent.Frame.Handler.Equipped.Value = selected.Value
						
							elseif script.Parent.Input.Value == "Mode 3" then	
								print("Equip to Last Equiped Light, Unequip to Last Equipped Tool")
							if script.Parent.Parent.Frame.Handler.LastEquippedTool.Value ~= nil then
								character.Humanoid:EquipTool(LastEquippedTool.Value)
								script.Parent.Parent.Frame.Handler.Equipped.Value = LastEquippedTool.Value
								script.Parent.Parent.Frame.Handler.Selected.Value = LastEquippedTool.Value
								script.Parent.Parent.Frame.Frame.ItemName.Text = LastEquippedTool.Value.Name
								script.Parent.Parent.Frame.Frame.ImageLabel.Image = LastEquippedTool.Value.TextureId
								script.Parent.Parent.Frame.Handler.Location.Value = player
						else 
							if script.Parent.Parent.Frame.Handler.LastEquippedTool.Value == nil then
								character.Humanoid:UnequipTools()
							end
						end
					end
				end
			end
		end
	end
end)

May I ask, are you changing the value from a serverscript or a local script? (ik that the script provided is local, but where are you changing the value from)

He he, silly me was replying to the wrong person in the thread, maybe check out what’s been written up there. :sweat_smile:

Also the function to switch modes is not the script (in this post), thats why it isn’t changing mode’s.

im going into my local player, player gui and change the value from there, and when i changed them it is going through because it prints somethings but for some reason it doesnt work in the script

So you are manually changing the value in studio?
The value needs to be changed locally (using a local script), so it can’t be done that way