ElseIf Statement not working

I have two scripts, one is local and triggers when a gui button is pressed, it will send an FireServer to a normal script. When it is fired it also gets sent with a string, depending on the string an elseif statement will let the code work, I have use printing and every bug finding thing I can think of, but it seems that nothing is wrong, but it does not work.

LOCAL SCRIPT (This is not the full script btw, just the part that matters)

local WoodenHookButton = ShopFrame.SframeClip.SFrame.WoodenHook

WoodenHookButton.MouseButton1Click:Connect(function()
	local WoodenHook = "WoodenHookWORK"
	Event:FireServer(WoodenHook)
end)

MAIN SCRIPT (This is the full script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.GoldBought

Event.OnServerEvent:Connect(function(Player,HookType)
	local Folder = Player:WaitForChild("leaderstats")
	local IntValue = Folder:WaitForChild("Coins")
	
	local Folder2 = Player:WaitForChild("GoldenHook")
	local Owned = Folder2:WaitForChild("Owned")
	
	local Folder3 = Player:WaitForChild("TPose")
	local Owned2 = Folder3:WaitForChild("Owned")
	
	local Folder4 = Player:WaitForChild("WoodenHook")
	local Owned3 = Folder4:WaitForChild("Owned")
	
	
	
 if HookType == "GoldenHook" then
		print("1")
	if Owned.Value == false then
		if IntValue.Value >= 1 then
			IntValue.Value = IntValue.Value - 1
				
				
			Owned.Value = true
           end
		end
		
	elseif HookType == "TPose" then
		if Owned2.Value == false then
			if IntValue.Value >= 1 then
				IntValue.Value = IntValue.Value - 1

				Owned2.Value = true
				
	elseif HookType == "WoodenHookWORK" then
		if Owned3.Value == false then
			if IntValue.Value >= 1 then
					IntValue.Value = IntValue.Value - 1
						
					Owned3.Value = true
						
	elseif HookType == "UpsideDown" then
					
					end	
				end
			end
		end
	end
end)

The structure of your conditional statements is the reason you have this issue. Specifically after the first elseif statement:

elseif HookType == "TPose" then
	if Owned2.Value == false then
		if IntValue.Value >= 1 then
			IntValue.Value = IntValue.Value - 1

			Owned2.Value = true
				
elseif HookType == "WoodenHookWORK" then
	if Owned3.Value == false then
		if IntValue.Value >= 1 then
			IntValue.Value = IntValue.Value - 1
						
			Owned3.Value = true

Do you see the issue? This is basically saying that if Int.Value is not:

IntValue.Value >= 1

then check if the HookType is equal to:

HookType == "WoodenHookWORK"

This will never be true since the code structured and indented correctly looks like this:

elseif HookType == "TPose" then
	if Owned2.Value == false then
		if IntValue.Value >= 1 then
			IntValue.Value = IntValue.Value - 1
			Owned2.Value = true
		elseif HookType == "WoodenHookWORK" then
		   -- Rest of code
		end
	end
end

Do you see it now? You need to correctly structure your code so the correct conditional statement is checked. Here is how you can fix your structure:

if HookType == "GoldenHook" then
	print("1")
	if Owned.Value == false then
		if IntValue.Value >= 1 then
			IntValue.Value = IntValue.Value - 1			
			Owned.Value = true
		end
	end	
elseif HookType == "TPose" then
	if Owned2.Value == false then
		if IntValue.Value >= 1 then
			IntValue.Value = IntValue.Value - 1
			Owned2.Value = true
		end
	end			
elseif HookType == "WoodenHookWORK" then
	if Owned3.Value == false then
		if IntValue.Value >= 1 then
			IntValue.Value = IntValue.Value - 1
			Owned3.Value = true
		end
	end
elseif HookType == "UpsideDown" then
-- Rest of code
end

Oooooooh. I completely missed that. Oopsie moment. Thank you for the help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.