Hey, this is what you can do.
A Factory Reset is supposed to fix this issue . Assuming that there is no on-going hardware errors, you can try to isolate the problem by going to Safe Mode and testing the Wi-Fi connection again. To bring your phone to Safe Mode:
Switch Off your Samsung Galaxy S4 completely
Power it ON Again
While booting continuously keep pressing the left button beside the home button.
Continue to press it still the boot is completed and you can see the home screen
Once you’re in Safe Mode, pull down the notification bar and try to turn on Wi-Fi (it should). After the Wi-Fi is activated, press and hold the power button on the right side your phone then select “Restart”. After the phone reboots, the Wi-Fi feature should be working normally.
If this doesn’t help, try removing the battery while the phone is still on, then power on the phone again.
If not through again, try taking it to a technician there can be problems with the hardware.