Jump to content

Table usage, dimension change.


Recommended Posts

Hi guys! lately I've bumped into a problem in my script, which I really can't have any ideas how to solve, or where did I :Oed up with the logic.
It's not really the hardest thing to do, but I overcomplicated it somehow with the tables and stuff.
The script is basically simple, you can attach/detach to a vehicle with a button press, and when the car moves into different interior/dimension then your character will follow it there, but get's detached because I couldn't really make it work, bugged out with attach or something. But the important stuff is that the interior change is actually working somehow, but the dimension didn't changed, no matter how I done it.
 

local attachedPlayers = {}

function handleInteriorDimensionChange(oldInterior, newInterior, oldDimension, newDimension)
    local vehicle = source 
    local attachedElements = getAttachedElements(vehicle)

    for _, attachedElement in ipairs(attachedElements) do
        if getElementType(attachedElement) == "player" then
            local attachedPlayer = attachedElement
            triggerClientEvent("playerAnimationToClient", root, "", "", attachedPlayer)
            triggerClientEvent("stopUpdatingPlayerRotation", root, attachedPlayer)
            
            if oldInterior ~= newInterior then
                setElementInterior(attachedPlayer, newInterior)
                outputChatBox (inspect (attachedPlayer) .. "'s interior changed from " .. oldInterior .. " to " .. newInterior)
            end

            if oldDimension ~= newDimension then
                setElementDimension(attachedPlayer, newDimension)
                outputChatBox(inspect(attachedPlayer) .. "'s dimension changed from " .. oldDimension .. " to " .. newDimension)
            end

            detachElements(attachedPlayer, vehicle)
        
            setElementData(attachedPlayer, "attached", false)
            setElementData(attachedPlayer, "attachedVehicle", nil)
            setElementData(attachedPlayer, "attachmentPosition", nil)
            if attachmentPoint then
                attachmentPointsStatus[attachmentPoint] = false
            end
        end
    end
end

function getAttachedPlayers(vehicle)
    local attachedPlayers = {}
    local attachedElements = getAttachedElements(vehicle)
    for _, attachedElement in ipairs(attachedElements) do
        if getElementType(attachedElement) == "player" then
            table.insert(attachedPlayers, attachedElement)
        end
    end
    return attachedPlayers
end

 

Link to comment

Hi,

Would not the attached players follow the vehicle when switching dimension or interior if player is set as children of the vehicle.

I'm not sure if it works, could be worth to try.

Maybe attachElements is already setting element as children ...

-- NOT TESTED

function attachPlayerToVehicle(player, vehicle)
	if not getElementType(player) == "player" then return end
	if not getElementType(vehicle) == "vehicle" then return end
	attachElements(player, vehicle)
	setElementParent(player, vehicle)
end

 

edit :

I tried with runcode, attached myself to a vehicle, set myself children to this vehicle, set a different interior and dimension to the vehicle and I was following it.

I tried also wihout setting myself as children and I was not following.

 

You whole code can only be a short function :)

You could also override attachElements to handle any case by default.

Have a nice day

Edited by Mkl
  • Thanks 1
Link to comment

But it's also ruining it somehow, because player cannot be set back to it's root, because the way my script works, when the player attaches to vehicle, the player can be set to its child, but when it's not needed anymore the player cannot be set back to its root, or maybe I'm just using it wrong, I only need this parent stuff when the player is attached a.k.a part of the attachedPlayers table. The reason why I would want to reset it back to  root is because when the car changes interior/dimension the player because he's still an active element of the car, gets teleported there too, even if he is detached and etc.

Link to comment

Nevermind, figured it out in the end. :D

2 hours ago, Flashmyname said:

But it's also ruining it somehow, because player cannot be set back to it's root, because the way my script works, when the player attaches to vehicle, the player can be set to its child, but when it's not needed anymore the player cannot be set back to its root, or maybe I'm just using it wrong, I only need this parent stuff when the player is attached a.k.a part of the attachedPlayers table. The reason why I would want to reset it back to  root is because when the car changes interior/dimension the player because he's still an active element of the car, gets teleported there too, even if he is detached and etc.

 

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