Jump to content

Checking player position problem


Jprey

Recommended Posts

So im making a small script that should check if player is in a position and if is then add random amount of money, and i did it like this. 
It is working and all but whole server blocks cuz "while" is looping like crazy.
Any ideas how to make it optimized ?

Im also looking for a way to STOP task if player is dead or out of vector3 (mabye avoid while)

 

while (player.Position.DistanceTo2D(new Vector3(2835.48, 1527.3, 24.56)) < 15)
                    {
                        Task.Run(() =>
                        {
                            Task.Delay(1000).Wait();

                            Random rand = new Random();
                            int Money = rand.Next(17, 31);

                            CashAPI.API.AddCash(c, Money);
                        });

                    }

Tried this but it check if player is in position just at the beggining, and not checking if the player left position
 

Quote

if (player.Position.DistanceTo2D(new Vector3(2835.48, 1527.3, 24.56)) > 15)
                {
                    player.SendNotification("Niste u zoni");
                    return;
                } 
                if (player.Position.DistanceTo2D(new Vector3(2835.48, 1527.3, 24.56)) < 15)
                {

            		Task.Run(() =>
            		{
               			Task.Delay(5000).Wait();

                		Random rand = new Random();
                    	int Money = rand.Next(17, 31);

                    	CashAPI.API.AddCash(c, Money);
            		});
            	}

 

 

Edited by Jprey
edit
Link to comment
Share on other sites

using GTANetworkAPI;
using System;
using System.Collections.Generic;
using System.Timers;




namespace Roleplay.Zone
{
	public class Zone : Script
	{


        public void MakeZone(Client c)
        {

            ColShape ZonaM = NAPI.ColShape.CreateCylinderColShape(new Vector3(2835.48, 1527.3, 24.56), 3, 4);
            Marker ZonaOne = NAPI.Marker.CreateMarker(21, new Vector3(2835.48, 1527.3, 24.56), new Vector3(), new Vector3(0.48, 0.3, 0.56), 5f, new Color(16, 78, 139, 100));
            ZonaM.SetData("ZonaOne", ZonaOne);


        }


        [ServerEvent(Event.PlayerEnterColshape)]
        public void Event_EnterColshape(ColShape colshape, Client c)
        {
            Marker ZonaOne = colshape.GetData("ZonaOne");
            if(ZonaOne != null)
            {
                c.SendNotification("You entered zone");
                return;
            }
        }  
   }
}

I mean im doing something wrong here, why its not working ?

Edited by Jprey
Editing
Link to comment
Share on other sites

This should work fine, maybe you forgot to call MakeZone() ! I edited it a little so its called on resourcestart

	public class Zone : Script
	{

		[ServerEvent(Event.ResourceStart)]
		public void MakeZone()
		{

			ColShape ZonaM = NAPI.ColShape.CreateCylinderColShape(new Vector3(2835.48, 1527.3, 24.56), 3, 4);
			Marker ZonaOne = NAPI.Marker.CreateMarker(21, new Vector3(2835.48, 1527.3, 24.56), new Vector3(), new Vector3(0.48, 0.3, 0.56), 5f, new Color(16, 78, 139, 100));
			ZonaM.SetData("ZonaOne", ZonaOne);


		}


		[ServerEvent(Event.PlayerEnterColshape)]
		public void Event_EnterColshape(ColShape colshape, Client c)
		{		

			Marker ZonaOne = colshape.GetData("ZonaOne");
			if (ZonaOne != null)
			{
				c.SendNotification("You entered zone");
				return;
			}
		}

		[ServerEvent(Event.PlayerExitColshape)]
		public void Event_ExitColshape(ColShape colshape, Client c)
		{	
			Marker ZonaOne = colshape.GetData("ZonaOne");
			if (ZonaOne != null)
			{
				c.SendNotification("You left zone");
				return;
			}
		}
	}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...