Jump to content

[HELP] Modify glue script to allow weapon switching.


UTurn

Recommended Posts

Since weapon switching can't be done while glued to a vehicle, I tried to modify a glue script made by uPrell.

I made 2 functions to change weapons that are bound to Q and E just as X is bound to 'glue'.

Somewhere online I read I could still set the players weapon slot so I thought it would work.

Either I made a simple mistake in the code or I'm going about this wrong.

Did plenty searching and couldn't find anything that could help.

Here is the modified script, if you could tell me what I'm doing wrong or how I could do this it'd be greatly appreciated.

glue.lua [Client] (This is the only file I modified)

  
-- Move backwards through the weapon slots. 
function prevWeapon() 
    local player = getLocalPlayer() 
    local slot = getPedWeaponSlot(player) 
     
    if (slot == 0) then 
        slot = 12 
    else 
        slot = slot - 1 
    end 
     
    setPedWeaponSlot(player, slot) 
end 
  
-- Move forward through the weapon slots. 
function nextWeapon() 
    local player = getLocalPlayer() 
    local slot = getPedWeaponSlot(player) 
     
    if (slot == 12) then 
        slot = 0 
    else 
        slot = slot + 1 
    end 
     
    setPedWeaponSlot(player, slot) 
end 
  
-- Glue the player to the vehicle. 
function glue() 
    local player = getLocalPlayer() 
    if not getPedOccupiedVehicle(player) then 
        local vehicle = getPedContactElement(player) 
        if getElementType(vehicle) == "vehicle" then 
             
            local px, py, pz = getElementPosition(player) 
            local vx, vy, vz = getElementPosition(vehicle) 
            local sx = px - vx 
            local sy = py - vy 
            local sz = pz - vz 
             
            local rotpX = 0 
            local rotpY = 0 
            local rotpZ = getPedRotation(player) 
             
            local rotvX,rotvY,rotvZ = getElementRotation(vehicle) 
             
            local t = math.rad(rotvX) 
            local p = math.rad(rotvY) 
            local f = math.rad(rotvZ) 
             
            local ct = math.cos(t) 
            local st = math.sin(t) 
            local cp = math.cos(p) 
            local sp = math.sin(p) 
            local cf = math.cos(f) 
            local sf = math.sin(f) 
             
            local z = ct*cp*sz + (sf*st*cp + cf*sp)*sx + (-cf*st*cp + sf*sp)*sy 
            local x = -ct*sp*sz + (-sf*st*sp + cf*cp)*sx + (cf*st*sp + sf*cp)*sy 
            local y = st*sz - sf*ct*sx + cf*ct*sy 
             
            local rotX = rotpX - rotvX 
            local rotY = rotpY - rotvY 
            local rotZ = rotpZ - rotvZ 
             
            local slot = getPedWeaponSlot(player) 
             
            --outputDebugString("gluing ".. getPlayerName(player) .." to " .. getVehicleName(vehicle) .. "(offset: "..tostring(x)..","..tostring(y)..","..tostring(z).."; rotation:"..tostring(rotX)..","..tostring(rotY)..","..tostring(rotZ)..")") 
             
            triggerServerEvent("gluePlayer", player, slot, vehicle, x, y, z, rotX, rotY, rotZ) 
             
            unbindKey("x","down",glue) 
            bindKey("x","down",unglue) 
            bindKey("jump","down",unglue) 
             
            bindKey("q", "down", prevWeapon) 
            bindKey("e", "down", nextWeapon) 
        end 
    end 
end 
  
-- Unglue the player from the vehicle 
function unglue() 
    local player = getLocalPlayer() 
    triggerServerEvent("ungluePlayer", player) 
    unbindKey("jump","down",unglue) 
    unbindKey("x","down",unglue) 
    bindKey("x","down",glue) 
     
    unbindKey("q", "down", prevWeapon) 
    unbindKey("e", "down", nextWeapon) 
end 
  
addCommandHandler("glue", glue) 
addCommandHandler("unglue", unglue) 
  
bindKey("x","down",glue) 
  

glueS.lua [server]

function gluePlayer(slot, vehicle, x, y, z, rotX, rotY, rotZ) 
    attachElements(source, vehicle, x, y, z, rotX, rotY, rotZ) 
    setPedWeaponSlot(source, slot) 
end 
  
function ungluePlayer() 
    detachElements(source) 
end 
  
addEvent("gluePlayer",true) 
addEvent("ungluePlayer",true) 
addEventHandler("gluePlayer", getRootElement(), gluePlayer) 
addEventHandler("ungluePlayer", getRootElement(), ungluePlayer) 
  

The code doesn't throw any warnings or errors, but it doesn't do anything.

Thanks in advance for any help you can give.

Link to comment
Most of the time 70% of the slots are empty.

So the weapon won't be changed. (because you don't have a weapon in that slot)

So instead of just moving to the next slot, I need to check for the next slot that actually contains a weapon, and then set that slot? How can I check if a weapon slot actually contains a weapon? I looked through the ped functions in the wiki and I can only find a get and set method for the slot, I haven't found a function that can check the contents of a particular slot.

Link to comment
if getPedWeapon ( player,slot ) then 

Thank you IIYAMA, it's working perfect. Here's the code for anybody who does some searching on how to do this.

glue_c.lua [Client]

  
-- Move backwards through the weapon slots. 
function prevWeapon() 
    local wep = nil 
    local slot = getPedWeaponSlot(localPlayer) 
    local slotHasWep = false 
     
    -- Keep looking backwards for a slot that contains a weapon. 
    while (slotHasWep == false) do 
        -- Keep 'slot' in the range of 0-12 and decrement it. 
        if (slot == 0) then 
            slot = 12 
        else 
            slot = slot - 1 
        end 
         
        if (slot == 0) then 
            -- Allow the player to select fists. 
            slotHasWep = true 
            break 
        else 
            -- Check if there is a weapon and if it has any ammo. 
            wep = getPedWeapon(localPlayer, slot) 
            slotHasWep = ((wep ~= 0) and (getPedTotalAmmo(localPlayer, slot) ~= 0)) 
            if (slotHasWep) then break end 
        end 
    end 
     
    setPedWeaponSlot(localPlayer, slot) 
end 
  
-- Move forward through the weapon slots. 
function nextWeapon() 
    local wep = nil 
    local slot = getPedWeaponSlot(localPlayer) 
    local slotHasWep = false 
     
    -- Keep looking forwards for a slot that contains a weapon. 
    while (slotHasWep == false) do 
        -- Keep 'slot' in the range of 0-12 and increment it. 
        if (slot == 12) then 
            slot = 0 
        else 
            slot = slot + 1 
        end 
         
        if (slot == 0) then 
            -- Allow the player to select fists. 
            slotHasWep = true 
            break 
        else 
            -- Check if there is a weapon and if it has any ammo. 
            wep = getPedWeapon(localPlayer, slot) 
            slotHasWep = ((wep ~= 0) and (getPedTotalAmmo(localPlayer, slot) ~= 0)) 
            if (slotHasWep) then break end 
        end 
    end 
     
    setPedWeaponSlot(localPlayer, slot) 
end 
  
-- Glue the player to the vehicle. 
function glue() 
    if not getPedOccupiedVehicle(localPlayer) then 
        local vehicle = getPedContactElement(localPlayer) 
         
        if getElementType(vehicle) == "vehicle" then 
            local px, py, pz = getElementPosition(localPlayer) 
            local vx, vy, vz = getElementPosition(vehicle) 
            local sx = px - vx 
            local sy = py - vy 
            local sz = pz - vz 
             
            local rotpX = 0 
            local rotpY = 0 
            local rotpZ = getPedRotation(localPlayer) 
             
            local rotvX,rotvY,rotvZ = getElementRotation(vehicle) 
             
            local t = math.rad(rotvX) 
            local p = math.rad(rotvY) 
            local f = math.rad(rotvZ) 
             
            local ct = math.cos(t) 
            local st = math.sin(t) 
            local cp = math.cos(p) 
            local sp = math.sin(p) 
            local cf = math.cos(f) 
            local sf = math.sin(f) 
             
            local z = ct*cp*sz + (sf*st*cp + cf*sp)*sx + (-cf*st*cp + sf*sp)*sy 
            local x = -ct*sp*sz + (-sf*st*sp + cf*cp)*sx + (cf*st*sp + sf*cp)*sy 
            local y = st*sz - sf*ct*sx + cf*ct*sy 
             
            local rotX = rotpX - rotvX 
            local rotY = rotpY - rotvY 
            local rotZ = rotpZ - rotvZ 
             
            local slot = getPedWeaponSlot(localPlayer) 
             
            triggerServerEvent("gluePlayer", localPlayer, slot, vehicle, x, y, z, rotX, rotY, rotZ) 
             
            unbindKey("x", "down", glue) 
            bindKey("x", "down", unglue) 
            bindKey("jump", "down", unglue) 
             
            bindKey("q", "down", prevWeapon) 
            bindKey("e", "down", nextWeapon) 
        end 
    end 
end 
  
-- Unglue the player from the vehicle 
function unglue() 
    triggerServerEvent("ungluePlayer", localPlayer) 
    unbindKey("jump", "down", unglue) 
    unbindKey("x", "down", unglue) 
    bindKey("x", "down", glue) 
     
    unbindKey("q", "down", prevWeapon) 
    unbindKey("e", "down", nextWeapon) 
end 
  
addCommandHandler("glue", glue) 
addCommandHandler("unglue", unglue) 
  
bindKey("x", "down", glue) 
  

glue_s.lua [server]

function gluePlayer(slot, vehicle, x, y, z, rotX, rotY, rotZ) 
    attachElements(source, vehicle, x, y, z, rotX, rotY, rotZ) 
    setPedWeaponSlot(source, slot) 
end 
  
function ungluePlayer() 
    detachElements(source) 
end 
  
addEvent("gluePlayer",true) 
addEvent("ungluePlayer",true) 
addEventHandler("gluePlayer", getRootElement(), gluePlayer) 
addEventHandler("ungluePlayer", getRootElement(), ungluePlayer) 

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