Jump to content

[HELP] Project of a basic turret


Emanuel

Recommended Posts

First of all, I am a beginner in this gigantic world, and sorry for my ignorance.

I have some bugs, that I want to be solved.

Bugs:

- The project is not synchronized. Each player gets attacked by his own turret, and not from the one I have created.

- When I use the command /turret it creates one on me, and other players have one on their head. For example, if anyone uses /turret, I will have a turret on me, which doesn't make sence.

Here is the project :

Client :

function turret() 
    x, y, z = getElementPosition(getLocalPlayer()) 
    weapon = createWeapon("m4", x, y, z+1.5) 
    setWeaponClipAmmo ( weapon,99999) 
    setWeaponState ( weapon,"firing") 
        setWeaponTarget ( weapon, true ) 
        setWeaponTarget ( weapon ,getLocalPlayer(), 255 )       
end 
addEvent("turret", true) 
addEventHandler("turret", root, turret) 
  
function destroy() 
        destroyElement (weapon) 
end 
addEvent("destroy", true) 
addEventHandler("destroy", root, destroy) 

Server :

function turret() 
        triggerClientEvent(root, "turret", root, x, y, z) 
end 
addCommandHandler("turret", turret) 
  
function destroy() 
        triggerClientEvent(root, "destroy", root, x, y, z) 
end 
addCommandHandler("end", destroy) 

Meta :

<meta> 
    <script src="client.lua" type="client" /> 
    <script src="server.lua" type="server" /> 
    <min_mta_version client="1.3.0-9.04555"></min_mta_version> 
</meta> 
  

I would appreciate if you could fix it. thanks.

Link to comment

you can create weapon with same command but problem if player joined server after create weapon will don't see him

-->  Client 
local weapons = { } 
  
function turret(thePlayer) 
    if not (weapon[thePlayer]) then 
        local x, y, z = getElementPosition(thePlayer) 
        weapon[thePlayer] = createWeapon("m4", x, y, z) 
        setWeaponState(weapon[thePlayer],"firing") 
        attachElements(weapon[thePlayer], thePlayer, 0, 0, 1.5) 
    else 
        destroyElement (weapon[thePlayer]) 
        weapon[thePlayer] = nil 
    end 
end 
addEvent("turret", true) 
addEventHandler("turret", root, turret) 
  
-->  Server 
function turret(thePlayer) 
        triggerClientEvent(root, "turret", thePlayer) 
end 
addCommandHandler("turret", turret) 

Link to comment

Use a loop to get the all the players to fire at, then add a check to see if the player isn't yourself and I think you'll be fine.

You're using getLocalPlayer(), which gets the player who triggered the function, so yea, it's obviously gonna shoot at you :P

Link to comment
you can create weapon with same command but problem if player joined server after create weapon will don't see him
-->  Client 
local weapons = { } 
  
function turret(thePlayer) 
    if not (weapon[thePlayer]) then 
        local x, y, z = getElementPosition(thePlayer) 
        weapon[thePlayer] = createWeapon("m4", x, y, z) 
        setWeaponState(weapon[thePlayer],"firing") 
        attachElements(weapon[thePlayer], thePlayer, 0, 0, 1.5) 
    else 
        destroyElement (weapon[thePlayer]) 
        weapon[thePlayer] = nil 
    end 
end 
addEvent("turret", true) 
addEventHandler("turret", root, turret) 
  
-->  Server 
function turret(thePlayer) 
        triggerClientEvent(root, "turret", thePlayer) 
end 
addCommandHandler("turret", turret) 

This isn't working :/

Use a loop to get the all the players to fire at, then add a check to see if the player isn't yourself and I think you'll be fine.

You're using getLocalPlayer(), which gets the player who triggered the function, so yea, it's obviously gonna shoot at you :P

Do you even know how noob I am in this ? lol .I really need help in this xD

PS : Someone said that I could use triggerServerEvent("eventName", root, theResultOfFunction)

Any suggestion ?

Link to comment
you can create weapon with same command but problem if player joined server after create weapon will don't see him
-->  Client 
local weapons = { } 
  
function turret(thePlayer) 
    if not (weapon[thePlayer]) then 
        local x, y, z = getElementPosition(thePlayer) 
        weapon[thePlayer] = createWeapon("m4", x, y, z) 
        setWeaponState(weapon[thePlayer],"firing") 
        attachElements(weapon[thePlayer], thePlayer, 0, 0, 1.5) 
    else 
        destroyElement (weapon[thePlayer]) 
        weapon[thePlayer] = nil 
    end 
end 
addEvent("turret", true) 
addEventHandler("turret", root, turret) 
  
-->  Server 
function turret(thePlayer) 
        triggerClientEvent(root, "turret", thePlayer) 
end 
addCommandHandler("turret", turret) 

This isn't working :/

Use a loop to get the all the players to fire at, then add a check to see if the player isn't yourself and I think you'll be fine.

You're using getLocalPlayer(), which gets the player who triggered the function, so yea, it's obviously gonna shoot at you :P

Do you even know how noob I am in this ? lol .I really need help in this xD

PS : Someone said that I could use triggerServerEvent("eventName", root, theResultOfFunction)

Any suggestion ?

NOTE : It is something like this. It is set on a determinated position, not attacked to a player, or a car.

MUWskNm.jpg?1

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