The "if" statement in my function doesn't seem to work

I want to launch a RemoteEvent dependend on the Player’s Value in his PlayerScripts as soon as an UI-Button is pressed. I used the “if/elseif” staments to determine what Value the Player has and which Remotefunction should be fired(activated).

My issue is that the RemoteEvents won’t fire(activate). I know that because i put a “print” statement into each “if/elseif” statement which won’t print anything into the output meaning that the RemoteEvent won’t fire either.

I also know that the RemoteEvents themselves work perfectly fine because i was able to fire them in different localscripts before without any issue.

This is the function btw:

pb.MouseButton1Click:Connect(function()
	print("function")
	if val == 1 then
		print("Infantry")
		Infantry:FireServer()
	elseif val == 1.5 then
		print("Infanterie")
		Infanterie:FireServer()
	elseif val == 2 then
		Assault:FireServer()
		print("Assault")
	elseif val == 2.5 then
		Sturmsoldat:FireServer()
		print("Sturmsoldat")
	elseif val == 3 then
		Support:FireServer()
		print("Support")
	elseif val == 3.5 then
		Unterst:FireServer()
		print("Unterstützer")
	elseif val == 4 then
		Sniper:FireServer()
		print("Sniper")
	elseif val == 4.5 then
		Scharfschuetze:FireServer()
		print("Scharfschütze")
	end
	game.StarterGui.Deserteur.FDesert1.Visible = false
	cc.CameraType = Enum.CameraType.Custom
	--workspace.Menu.GPart.BillboardGui.Frame.ImageLabel.Visible = false
	--workspace.Menu.FPart.BillboardGui.Frame.ImageLabel.Visible = false
	--workspace.Menu.GPart.BillboardGui.Frame.Visible = false
	--workspace.Menu.FPart.BillboardGui.Frame.Visible = false
	pb:Destroy()
	bg:Destroy()
	l:Destroy()
	WL:Destroy()
	script.Parent.Parent.Inventory.Enabled = true
	game:GetService("UserInputService").MouseIconEnabled = false
	
end)

First, can you print val?
Second, sorry, but the code is hella unoptimal. You can easily use dictionaries to avoid those long checks. For example:

local valEvents = {[1] = Infantry, [1.5] = Infanterie, ... }

...

pb.MouseButtonClick1:Connect(function()
 print("function")
 local ev = valEvents[val]
 if ev ~= nil then
  print(ev.Name)
  ev:FireServer()
 end

 ...

end)
1 Like

Thanks for the way less complicated script and yes, I can print val but it stays zero and wont change eventhough the value itsself has changed.

Perhaps you just did not make a function that changes this value?

1 Like

I made it so that when a “Play” Button (which only appears once when you join) gets clicked you get the standard value for the team your in. And then you can
choose your weapon which also changes the value before you finally press deploy to enter the game with the loadout/value you chose.
And if i check the Players value before the player deploys its not zero eventhough it says 0 afterwards in the output.

Could you send the code? (can in DM)

1 Like

Nvm! I got it to work by instead of making the value a module exterior of the script, I made the value a value inside of the script.

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