Function isn't working properly

//SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.5.0 < 0.9.0;

contract lottery{

bool public isLottery;

address manager;

address[] public participants;

constructor(){

    manager=msg.sender;

}

modifier onlyManager(){

    require(msg.sender==manager,"Only Manager can access this");

    _;

}

function pay() public payable{

    require(!isLottery, "Lottery has ended");

    require(msg.value>=2 ether,"Donation must be greater than 0.99 ether");

    participants.push(msg.sender);

}

function getBalance()public view  returns(uint){

    require(!isLottery, "Lottery has ended");

    return address(this).balance;

}

function random() internal view returns(uint){

    require(!isLottery, "Lottery has ended");

    return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, participants.length)));

}

function pickWinner()public onlyManager{

    require(!isLottery, "Lottery has ended");

    require(participants.length>3);

    uint count=random();

    uint winner=count%participants.length;

    payable(participants[winner]).transfer(getBalance());

    isLottery=true;

    delete participants;

 }

 function startLottery()public onlyManager{

     isLottery=false;

 }

 function showParticipants() public view returns(uint){

     return participants.length;

 }

}

This is a Lottery smart contract-

so whenever I use the executefunction to call the pickWinner function of my smart contract the contract always sends the contract balance to my account(owner account) and not to the actual winner’s account. Can anyone help me why this is happening.

Has the owner also participated in the lottery?
If yes then he probably got lucky. I tested your random winner function with return data. It is giving me a random winner every time I call this function.

function pickWinnerTest()public onlyManager view returns(uint){
    require(!isLottery, "Lottery has ended");
    require(participants.length>3);
    uint count=random();
    uint winner=count%participants.length;
    return winner;
 }

Yes the owner has also participated in the lottery but everytime i click PickWInner it sends the contract balance to owner. Why won’t you try to call the pickWInner function and then see what’s happening.

I tried PickWinner aswell. I received different winners on every call.

Does you have used the executeFunction from Moralis?

Tested it on remix Javascript VM only.

Yes mate on remix it’s working properly, the problem is occuring when i m trying to call it using moralis api in front-end. Can you just spend some time to call the function using moralis and then check is it wokring or not as in my case it’s not working properly.

What are the options ( abi and contractaddress) ? I can try that out

Since the randomness math is happening on solidity itself and the pickWinner() function does not take any parameter, So I doubt if that is an issue from frontend.

For us to check on the frontend can you deploy the contract without the onlyManager modifier, reduce the msg value to 0.001 and share the ABI, contract address and chain.
So it will be easy for us to test.