Jump to content

Server and client script


3eBpA

Recommended Posts

  • Moderators

For teams, you can use the internal team system from MTA (as Bonsai suggested) because they are synced on both sides.

But more generally, if you want to send a copy of a variable to the other side, you will have to use:

triggerClientEvent --to send to the client 
triggerServerEvent --to send to the server 

You also can use this trick:

You can use

setElementData(resourceRoot, "myData", myValue) 

The element datas are synced between both sides.

You can get the value of "myData" by doing:

getElementData(resourceRoot, "myData") 

Regards,

Citizen

Link to comment

well, why my code doesn't work then ? : O

function Stun(attacker) 
if getPlayerTeam(attacker) == police and getPlayerTeam(source) ~= police and getElementModel(attacker) == 3 
    setElementFrozen(source) 
end 
end 
addEventHandler("OnClientWeaponFire", getRootElement(), Stun) 

it should stun guy with weaponID 3 if his team != police but it doesn't works, maybe because team creation is written in server.lua ?

Link to comment
  • Moderators

it's "onClientWeaponFire" not "OnClientWeaponFire".

Tip:

- To debug this problem, you could add an outputChatBox like this:

function Stun(attacker) 
    outputChatBox("Fire !") 
    if getPlayerTeam(attacker) == police and getPlayerTeam(source) ~= police and getElementModel(attacker) == 3 
        setElementFrozen(source) 
    end 
end 
addEventHandler("OnClientWeaponFire", getRootElement(), Stun) 

And you would know the problem wasn't from the team functions but the event.

Regards,

Citizen

Link to comment
it's "onClientWeaponFire" not "OnClientWeaponFire".

Tip:

- To debug this problem, you could add an outputChatBox like this:

function Stun(attacker) 
    outputChatBox("Fire !") 
    if getPlayerTeam(attacker) == police and getPlayerTeam(source) ~= police and getElementModel(attacker) == 3 
        setElementFrozen(source) 
    end 
end 
addEventHandler("OnClientWeaponFire", getRootElement(), Stun) 

And you would know the problem wasn't from the team functions but the event.

Regards,

Citizen

I've changed OnClientWeaponFire to onClientWeaponFire, I think the problem is in teams =\

Link to comment
  • Moderators

Wait, does this work ?! The wiki says the event isn't triggered with melee weapons, and 3 is a melee weapon so it shouldn't work :shock:.

See the note of the wiki page:

Note: This event is only for custom weapons that were created with createWeapon, for player held weapons use onClientPlayerWeaponFire.

So use onClientPlayerWeaponFire event instead. But, there is another important note:

This does not trigger for projectiles based, or melee weapons

and you are using the weaponID 3 so according to this list:

Weapon ids list

it is a melee weapon, so it won't work.

So what can we do now ?? :shock:

We can try to find another event, and I found this event: onClientPlayerDamage

This event is triggered whenever a player is damaged.

and it gives us an important parameter:

attacker: A player element representing the attacker

Great ! We can do it with this event:

function Stun( attacker ) 
    outputChatBox("Took damage !") 
    local policeTeam = getTeamFromName("police") 
    if getPlayerTeam(attacker) == policeTeam and getPlayerTeam(source) ~= policeTeam and getElementModel(attacker) == 3 then 
        outputChatbox("Frozen !") 
        setElementFrozen(source, true) 
    end 
end 
addEventHandler("onClientPlayerDamage", localPlayer, Stun) 

Tell me if it works.

Regards,

Citizen

Link to comment
Wait, does this work ?! The wiki says the event isn't triggered with melee weapons, and 3 is a melee weapon so it shouldn't work :shock:.

See the note of the wiki page:

Note: This event is only for custom weapons that were created with createWeapon, for player held weapons use onClientPlayerWeaponFire.

So use onClientPlayerWeaponFire event instead. But, there is another important note:

This does not trigger for projectiles based, or melee weapons

and you are using the weaponID 3 so according to this list:

Weapon ids list

it is a melee weapon, so it won't work.

So what can we do now ?? :shock:

We can try to find another event, and I found this event: onClientPlayerDamage

This event is triggered whenever a player is damaged.

and it gives us an important parameter:

attacker: A player element representing the attacker

Great ! We can do it with this event:

function Stun( attacker ) 
    outputChatBox("Took damage !") 
    local policeTeam = getTeamFromName("police") 
    if getPlayerTeam(attacker) == policeTeam and getPlayerTeam(source) ~= policeTeam and getElementModel(attacker) == 3 then 
        outputChatbox("Frozen !") 
        setElementFrozen(source, true) 
    end 
end 
addEventHandler("onClientPlayerDamage", localPlayer, Stun) 

Tell me if it works.

Regards,

Citizen

thanks, I'm gonna try this tomorrow

Link to comment
Normaly, you shold do this:
  
police = getTeamFromName("Police - Team - Name - Here") 
  

After that, if the team exists, it should work.

thank you man, your really save me from sleepless nights :D

Told you that hours ago.

If you would pay more attention to replies you could have saved us from trying to fix a problem that is already fixed.

Link to comment
Wait, does this work ?! The wiki says the event isn't triggered with melee weapons, and 3 is a melee weapon so it shouldn't work :shock:.

See the note of the wiki page:

Note: This event is only for custom weapons that were created with createWeapon, for player held weapons use onClientPlayerWeaponFire.

So use onClientPlayerWeaponFire event instead. But, there is another important note:

This does not trigger for projectiles based, or melee weapons

and you are using the weaponID 3 so according to this list:

Weapon ids list

it is a melee weapon, so it won't work.

So what can we do now ?? :shock:

We can try to find another event, and I found this event: onClientPlayerDamage

This event is triggered whenever a player is damaged.

and it gives us an important parameter:

attacker: A player element representing the attacker

Great ! We can do it with this event:

function Stun( attacker ) 
    outputChatBox("Took damage !") 
    local policeTeam = getTeamFromName("police") 
    if getPlayerTeam(attacker) == policeTeam and getPlayerTeam(source) ~= policeTeam and getElementModel(attacker) == 3 then 
        outputChatbox("Frozen !") 
        setElementFrozen(source, true) 
    end 
end 
addEventHandler("onClientPlayerDamage", localPlayer, Stun) 

Tell me if it works.

Regards,

Citizen

yep, works, thanks man

Link to comment

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...