Jump to content

[HELP] Get element from a Table?


Recommended Posts

is it possible to get an element from a table by type , for example when i get attached elements to my car , i attached a sound element to my car. but get attached elements returns all the attached elements in to a table ( as i know) , is there a way to get the sound element from the table ???

Link to comment
  • Moderators

When you attach the Sound Element to the Vehicle Element save this into a own table.

 

Example:

local sounds = {}

function attachSoundToVehicle(vehicleElement, soundElement)
	attachElements(vehicleElement, soundElement)
  	sounds[vehicleElement] = soundElement
end

function getVehicleAttachedSound(vehicleElement)
	if isElement(sounds[vehicleElement]) then -- have attached sound
  		return sounds[vehicleElement] -- This is the sound element: sounds[vehicleElement]
 	end
end

 

Link to comment

ty but will its work when the sound element is attached to more than one vehicle?? ill just post my script here. the problem with it is that when i use stop sound it stops the sound from all vehicles , so i need to get the element from the player vehicle itself so i will just get the sound element on his car. and destroy it to stop the first radio and so on.

-- client
tag = off
local change = 0
function noRadio() 
 setRadioChannel(0) 
 addEventHandler('onClientPlayerRadioSwitch', getRootElement(), function() cancelEvent() end ) 
end 
function change()
if change == 0 then
change = 1
play1()
else
change2()
end
end
function change2()
if change == 1 then
change = 2
play2()
else
change3()
end
end
function change3()
if change == 2 then
change = 3
play3()
else
change4()
end
end
function change4()
if change == 3 then
change = 0
play4()
else
change = 0
change()
end
end
addEvent( "change", true )
addEventHandler( "change", localPlayer, change )

function play()
noRadio()
outputChatBox("Radio off!!" )
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local off = getAttachedElements ( vehicle )
destroyElement(off) 
end
addEvent( "play", true )
addEventHandler( "play", localPlayer, play )

function play1()
tag = "Hi-Roam HipHop"
noRadio()
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
destroyElement(attachedElements) 
sound = playSound3D( "http://us3.internet-radio.com:8313/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
dxDrawText("Hi-Roam HipHop", 492, 10, 907, 61, tocolor(255, 255, 255, 255), 3.00, "default", "left", "top", true, false, false, false, false)
end
addEvent( "r1", true )
addEventHandler( "r1", localPlayer, play1 )

function play2()
tag = "Hi-Roam Metal"
noRadio()
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
destroyElement(attachedElements) 
sound = playSound3D( "http://188.40.32.140:8061/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
dxDrawText("Hi-Roam Metal", 492, 10, 907, 61, tocolor(255, 255, 255, 255), 3.00, "default", "left", "top", true, false, false, false, false)
tag()
end
addEvent( "r2", true )
addEventHandler( "r2", localPlayer, play2 )

function play3()
tag = "Hi-Roam Pop"
noRadio()
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
destroyElement(attachedElements) 
dxDrawText("Hi-Roam Pop", 492, 10, 907, 61, tocolor(255, 255, 255, 255), 3.00, "default", "left", "top", true, false, false, false, false)
sound = playSound3D( "http://uk7.internet-radio.com:8040/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
tag()
end
addEvent( "r3", true )
addEventHandler( "r3", localPlayer, play3 )

function play4()
tag = "Hi-Roam Dubstep"
noRadio()
channel = dxDrawText("Hi-Roam Dubstep", 492, 10, 907, 61, tocolor(255, 255, 255, 255), 3.00, "default", "left", "top", true, false, false, false, false)
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
destroyElement(attachedElements) 
sound = playSound3D( "http://83.142.226.45:25700/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
tag()
end
addEvent( "r4", true )
addEventHandler( "r4", localPlayer, play4 )

function tag()
dxDrawText("Hi-Roam Pop", 492, 10, 907, 61, tocolor(255, 255, 255, 255), 3.00, "default", "left", "top", true, false, false, false, false)
end

ignore the function tag() , i didnt clean up this script so it has a lot of useless stuff. so this is how it looks like and from the server side i just call the client side function "change" and dependiing on the number it scrolls through , but the sound doesnt stop at all :(. and yeah i am not much of a good coder :(:( so pls help me out :)

Link to comment
tag = "Radio off" -- Channel Name
local change = -1 -- Channel ID

-- Turns off Default SA Radio
function noRadio() 
 setRadioChannel(0) 
 addEventHandler('onClientPlayerRadioSwitch', getRootElement(), function() cancelEvent() end ) 
end 

-- Changing radio Stations, And checks which radio station is on ( it keeps adding to 0 , if channel is 0 then its means Radio is Off )

function change() -- Checks If Radio Was Off And Moves To Channel 1
if change == 0 then
change = 1
play1()
else
change2()
end
end

function change2() -- Checks If Radio Channel was 1 and moves To 2
if change == 1 then
change = 2
play2()
else
change3()
end
end

function change3() -- Checks If Radio Channel was 2 and moves To 3
if change == 2 then
change = 3
play3()
else
change4()
end
end

function change4() -- Checks If Radio Channel was 3 and moves To 0
if change == 3 then
change = -1 -- Resets Channel Back to 0
play4()
else
if change == -1 then
play5()
end
end
end

function change5() -- Checks If Radio Channel was 3 and moves To 0
if change == -1 then
change = 1 -- Resets Channel Back to 0
play()
else
end
end

addEvent( "change", true )
addEventHandler( "change", localPlayer, change )

-- Channels For the Radio 
-- Radio Off
function play()
tag = "Radio Off"
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
for i, element in ipairs(attachedElements) do
destroyElement(element) 
end
tag()
end
addEvent( "r0", true )
addEventHandler( "r0", localPlayer, play1 )

-- Channel 1
function play1()
tag = "HipHop"
noRadio()
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
for i, element in ipairs(attachedElements) do
destroyElement(element) 
end
sound = playSound3D( "http://us3.internet-radio.com:8313/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
end
addEvent( "r1", true )
addEventHandler( "r1", localPlayer, play1 )

-- Channel 2
function play2()
tag = "Metal"
noRadio()
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
for i, element in ipairs(attachedElements) do
destroyElement(element) 
end
sound = playSound3D( "http://188.40.32.140:8061/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
tag()
end
addEvent( "r2", true )
addEventHandler( "r2", localPlayer, play2 )

-- Channel 3
function play3()
tag = "Pop"
noRadio()
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
for i, element in ipairs(attachedElements) do
destroyElement(element) 
end
sound = playSound3D( "http://uk7.internet-radio.com:8040/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
tag()
end
addEvent( "r3", true )
addEventHandler( "r3", localPlayer, play3 )

-- Channel 4
function play4()
tag = "Dubstep"
noRadio()
local vehicle = getPedOccupiedVehicle(getLocalPlayer( ))
local x,y,z = getElementPosition( vehicle ) 
local attachedElements = getAttachedElements ( vehicle )
destroyElement(attachedElements) 
sound = playSound3D( "http://83.142.226.45:25700/listen.pls", x, y, z) 
setSoundMaxDistance( sound,50 )
attachElements ( sound, vehicle) 
tag() -- Suppose to Use OnClientRender Here But As Im not Working On the Test Anymore I just Called the function So that  I will know Channel Changed.
end
addEvent( "r4", true )
addEventHandler( "r4", localPlayer, play4 )

-- Channel Name Display
function tag()
dxDrawText(tag, 492, 10, 907, 61, tocolor(255, 255, 255, 255), 3.00, "default", "left", "top", true, false, false, false, false)
end

there you go @Pirulax  , lets see if u can help me :)

 

Link to comment
  • Discord Moderators

Its still missing indentations.

Btw, yeah, use this script piece:

local  soundsByVeh = {}

function attachSoundToVehicle(vehicleElement, soundElement)
    attachElements(vehicleElement, soundElement)
    soundsByVeh[vehicleElement][soundElement] = soundElement
end

function getSoundsAttachedToVehicle(veh)
    return soundsByVeh[vehicleElement]
end

function removeAttachedSoundFromVehicle(veh, sound)
    soundsByVeh[vehicleElement][sound] = nil
end

 

Edited by Pirulax
  • Confused 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...