SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 25/05/2020, 05:27 PM   #1
XBRSAKOSKYX
Little Clucker
 
Join Date: Apr 2020
Posts: 34
Reputation: 0
Default Lock/Unlock OnPlayerKeyStateChange

Hi! Im doing a lock/unlock system with "OnPlayerKeyStateChange" but ofc i got errors as usual hahah Now of course im asking your help if we can fix this problem. I will put all codes here and tell me what im doing wrong. Ty

Code:
new PlayerCar[MAX_PLAYERS], LockedCar[1000];
new Text:Locked;
new MsgLocked[MAX_PLAYERS], LockedTimer[MAX_PLAYERS];
forward RemoveLock(playerid);
Code:
public OnPlayerConnect(playerid)
{
    PlayerCar[playerid] = -1;
    return 1;
}
Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
//----------------------------[LOCK/UNLOCK V]--------------------------------------------------
	if(newkeys == KEY_CROUCH)
	{
        if(!IsPlayerInAnyVehicle(playerid))
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle."); // player has to be in a vehicle
        if(GetPlayerVehicleSeat(playerid) != 0)
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver."); // player has to be a driver
        if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
        }
        if(!IsPlayerInAnyVehicle(playerid))
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle.");
        if(GetPlayerVehicleSeat(playerid) != 0)
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver.");
        PlayerCar[playerid] = -1; // tells server that this player don't have any locked vehicle
        LockedCar[GetPlayerVehicleID(playerid)] = 0; // unlocks current vehicle
        return 1;
    }
//-------
Code:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid) // if player is trying to enter locked vehicle and he is not bound to that vehicle
    {
        if(MsgLocked[playerid] == 1) // if there's already message showing for player
        {
            TextDrawHideForPlayer(playerid, Locked); // hides message
            MsgLocked[playerid] = 0; // there's no more any message regarding locked car
            KillTimer(LockedTimer[playerid]); // kills timer
        }
        TogglePlayerControllable(playerid, true); // stops player from entering vehicle (it's not his vehicle, why should he enter it?)
        Locked = TextDrawCreate(10.0, 250.0, "This Vehicle is Locked"); // pretty much self-explanatory
        TextDrawUseBox(Locked, 1);
        TextDrawBoxColor(Locked, 0x00000055);
        TextDrawTextSize(Locked, 125, 5);
        TextDrawShowForPlayer(playerid, Locked); // shows TextDraw to player
        LockedTimer[playerid] = SetTimer("RemoveLock", 4000, false); // sets timer for hiding TextDraw
        MsgLocked[playerid] = 1; // we have TextDraw showing
    }
    return 1;
}

public RemoveLock(playerid)
{
    if(MsgLocked[playerid] == 1) // if there's message showing for player
    {
        TextDrawHideForPlayer(playerid, Locked); // hides message
        MsgLocked[playerid] = 0; // there's no more any message regarding locked car
    }
}

public OnVehicleSpawn(vehicleid)
{
    LockedCar[vehicleid] = 0; // when vehicle has respawned (nobody has entered it for a while) server will unlock it
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerCar[i] == vehicleid)
            PlayerCar[i] = -1; // unbounds player from this vehicle so anyone can enter it
    }
    return 1;
}
XBRSAKOSKYX is offline   Reply With Quote
Old 25/05/2020, 08:20 PM   #2
Grim_
High-roller
 
Grim_'s Avatar
 
Join Date: Sep 2008
Location: United States
Posts: 2,897
Reputation: 118
Default Re: Lock/Unlock OnPlayerKeyStateChange

What errors are you getting?

You need to use bit operations for keys. Information regarding that can be found on the wiki.
pawn Code:
if(newkeys & KEY_CROUCH)

pawn Code:
if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
    }
Here you have an extra bracket. The logic currently will also always lock the vehicle, even after just unlocking it.
Grim_ is offline   Reply With Quote
Old 25/05/2020, 08:43 PM   #3
XBRSAKOSKYX
Little Clucker
 
Join Date: Apr 2020
Posts: 34
Reputation: 0
Default Re: Lock/Unlock OnPlayerKeyStateChange

Quote:
Originally Posted by Grim_ View Post
What errors are you getting?

You need to use bit operations for keys. Information regarding that can be found on the wiki.
pawn Code:
if(newkeys & KEY_CROUCH)

pawn Code:
if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
    }
Here you have an extra bracket. The logic currently will also always lock the vehicle, even after just unlocking it.
Can you make it complete? this are the erros i get now after changing "if(newkeys & KEY_CROUCH)" and removed the extra bracket:

Code:
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1121) : warning 225: unreachable code
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1252) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1254) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1255) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1256) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1257) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1258) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1264) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1265) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1266) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1267) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(1270) : error 010: invalid function or declaration
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(4128) : warning 219: local variable "Angle" shadows a variable at a preceding level
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(4237) : warning 219: local variable "Angle" shadows a variable at a preceding level
C:\Users\David\Desktop\SAMPserver\gamemodes\testing.pwn(4695) : warning 213: tag mismatch
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


11 Errors.
XBRSAKOSKYX is offline   Reply With Quote
Old 25/05/2020, 08:56 PM   #4
Grim_
High-roller
 
Grim_'s Avatar
 
Join Date: Sep 2008
Location: United States
Posts: 2,897
Reputation: 118
Default Re: Lock/Unlock OnPlayerKeyStateChange

I won't code it for you, but will help. Post your code with the error lines noted. It still seems you have an extra bracket.
Grim_ is offline   Reply With Quote
Old 25/05/2020, 08:59 PM   #5
XBRSAKOSKYX
Little Clucker
 
Join Date: Apr 2020
Posts: 34
Reputation: 0
Default Re: Lock/Unlock OnPlayerKeyStateChange

Quote:
Originally Posted by Grim_ View Post
I won't code it for you, but will help. Post your code with the error lines noted. It still seems you have an extra bracket.
Will post them tomorrow, just turned off the pc. Im glad that you will help me bcs i want i to work. My point of this code is that all kind of vehicles that player take from street or buy, it can be locked/unlocked by pressing the key. Im still new in coding etc, so thats why the most of the time im here😂
XBRSAKOSKYX is offline   Reply With Quote
Old 26/05/2020, 07:54 AM   #6
XBRSAKOSKYX
Little Clucker
 
Join Date: Apr 2020
Posts: 34
Reputation: 0
Default Re: Lock/Unlock OnPlayerKeyStateChange

Quote:
Originally Posted by Grim_ View Post
I won't code it for you, but will help. Post your code with the error lines noted. It still seems you have an extra bracket.
https://imgur.com/a/ykDtNAl here
XBRSAKOSKYX is offline   Reply With Quote
Old 26/05/2020, 08:27 AM   #7
Adamoneoone
Huge Clucker
 
Join Date: Sep 2014
Posts: 495
Reputation: 37
Default Re: Lock/Unlock OnPlayerKeyStateChange

You didn't post the whole thing. The error comes from the line 1114. Can you post the code and not a screenshot so that people can actually correct it?
__________________
If you want to have your own custom script, or even want to have one of our already released scripts, join our SCRIPTING SERVICES DISCORD, or send me a private message!
Adamoneoone is offline   Reply With Quote
Old 26/05/2020, 01:06 PM   #8
XBRSAKOSKYX
Little Clucker
 
Join Date: Apr 2020
Posts: 34
Reputation: 0
Default Re: Lock/Unlock OnPlayerKeyStateChange

Quote:
Originally Posted by Adamoneoone View Post
You didn't post the whole thing. The error comes from the line 1114. Can you post the code and not a screenshot so that people can actually correct it?
Bro i dont mind the warnings, everything is working okay with them only few misstakes that im going to fix soon, i need help with the errors. he said post the lines and errors and i did it the error starts at line 1245.
XBRSAKOSKYX is offline   Reply With Quote
Old 26/05/2020, 03:44 PM   #9
Grim_
High-roller
 
Grim_'s Avatar
 
Join Date: Sep 2008
Location: United States
Posts: 2,897
Reputation: 118
Default Re: Lock/Unlock OnPlayerKeyStateChange

Post your whole OnPlayerKeyStateChange callback.
Grim_ is offline   Reply With Quote
Old 26/05/2020, 04:12 PM   #10
XBRSAKOSKYX
Little Clucker
 
Join Date: Apr 2020
Posts: 34
Reputation: 0
Default Re: Lock/Unlock OnPlayerKeyStateChange

Quote:
Originally Posted by Grim_ View Post
Post your whole OnPlayerKeyStateChange callback.
Code:
//----------------------------------------------------------------------------//
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
	if(newkeys & KEY_JUMP)
	{
		if (MegaJump[playerid] == 1)
		{
		   	new Float:Jump[3];
		    GetPlayerVelocity(playerid, Jump[0], Jump[1], Jump[2]);
		    SetPlayerVelocity(playerid, Jump[0], Jump[1], Jump[2]+5);
			return 1;
		}
	}
	return 1;
}
//----------------------------[LOCK/UNLOCK V]--------------------------------------------------
	if(newkeys & KEY_CROUCH)
	{
        if(!IsPlayerInAnyVehicle(playerid))
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle."); // player has to be in a vehicle
        if(GetPlayerVehicleSeat(playerid) != 0)
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver."); // player has to be a driver
        if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
        }
        if(!IsPlayerInAnyVehicle(playerid))
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle.");
        if(GetPlayerVehicleSeat(playerid) != 0)
        return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver.");
        PlayerCar[playerid] = -1; // tells server that this player don't have any locked vehicle
        LockedCar[GetPlayerVehicleID(playerid)] = 0; // unlocks current vehicle
        return 1;
    }
XBRSAKOSKYX is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help in /lock and /unlock 123bob123 Server Support 1 17/07/2014 03:39 PM
Lock&Unlock bug [+REP] Jaber_Brown Scripting Help 5 15/03/2012 06:25 PM
Car lock/unlock known Help Archive 1 04/11/2010 10:24 AM
lock car and unlock help Kar Help Archive 10 22/06/2010 06:55 PM
[FilterScript] [FS] /lock and /unlock with automatic unlock timer Christian R. Filterscripts 4 26/10/2009 11:38 PM


All times are GMT. The time now is 05:51 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.