Jump to content

[NEED HELP] How to trigger a vehicle? Read to understand


lilwayn

Recommended Posts

5 hours ago, lilwayn said:

Hello @alex17" thanks for replying back.

I've fixed that problem, but i'm facing another issue. I wanna know how to trigger a variable from server to client, i tried alot but i couldnt make it working. is there any solution for that?

1. Create a new .lua file
2. Open meta and type <script src="yourfile.lua" type="shared" />

It will look like this
Shared File:
 

my_var = "test"

Server File:

addCommandHandler("s", function(p, c)
    outputChatBox(my_var, root, 255, 255, 255, true)
end)

Client File:

addCommandHandler("c", function()
    outputChatBox(my_var)
end)

I hope this is what you mean 😅

Link to comment
47 minutes ago, Hydra said:

1. Create a new .lua file
2. Open meta and type <script src="yourfile.lua" type="shared" />

It will look like this
Shared File:
 

my_var = "test"

Server File:

addCommandHandler("s", function(p, c)
    outputChatBox(my_var, root, 255, 255, 255, true)
end)

Client File:

addCommandHandler("c", function()
    outputChatBox(my_var)
end)

I hope this is what you mean 😅

Hello, i've tried your idea. When i printed the variable in server, it worked completely fine, but in client it doesnt seem to be working as everytime i try printing it keeps giving me a 0

I've added this in shared.lua 

plantedBombs = 0

this in server.lua 

function onHit(player)
    if (#m > 0 and #b > 0) then
        destroyElement(m[1])
        destroyElement(b[1])
        table.remove(m, 1)
        table.remove(b, 1)
    end
    for i, k in ipairs(Bomb2) do
		if (isElementAttached(Bomb2[i])) then
			detachElements(Bomb2[i], helic[1])
			plantedBombs = plantedBombs + 1
			outputChatBox("Bomb has been successfully installed, Do the same with the rest!", root, 34, 139, 34)
			outputChatBox(plantedBombs) ---- This returns what it should return
		end
    end
	if (plantedBombs == 5) then
		triggerEvent("stopMission", root, onHit)
		givePlayerMoney(player, 50000)
		outputChatBox("Congratulations, you have passed the mission and got $50,000 as a reward!", root, 34, 139, 34)
	end
end

This in client.lua

function renderTimer()
	local white = tocolor(255, 255, 255, 255)
	if (isTimer(startTimer)) then
		local remaining = getTimerDetails(startTimer)
		local remaining = remaining / 1000
		outputChatBox(plantedBombs) ---- Keeps printing a zero
		dxDrawText("Delievered Bombs : "..plantedBombs.."", 1162, 654, screenW, 711, white, 1.3, "default-bold", "right", "bottom")
		dxDrawText("Time Remaining : "..math.floor(remaining / 60)..":"..math.floor(remaining % 60).."", 1228, 725, screenW, 740, white, 1.3, "default-bold", "right", "bottom")
	end
end

Any solution?

Link to comment
--serverside
function onHit(player)
    if (#m > 0 and #b > 0) then
        destroyElement(m[1])
        destroyElement(b[1])
        table.remove(m, 1)
        table.remove(b, 1)
    end
    for i, k in ipairs(Bomb2) do
        if (isElementAttached(Bomb2[i])) then
            detachElements(Bomb2[i], helic[1])
            plantedBombs = plantedBombs + 1
            outputChatBox("Bomb has been successfully installed, Do the same with the rest!", root, 34, 139, 34)
            outputChatBox(plantedBombs) ---- This returns what it should return
            triggerClientEvent(player, "updatePlantedBombs", root, plantedBombs)
        end
    end
    if (plantedBombs == 5) then
        triggerEvent("stopMission", root, onHit)
        givePlayerMoney(player, 50000)
        outputChatBox("Congratulations, you have passed the mission and got $50,000 as a reward!", root, 34, 139, 34)
    end
end
--clientside
addEvent("updatePlantedBombs", true)
addEventHandler("updatePlantedBombs", root, function(plantedBombsFromServer)
    plantedBombs = plantedBombsFromServer
end)

function renderTimer()
    local white = tocolor(255, 255, 255, 255)
    if (isTimer(startTimer)) then
        local remaining = getTimerDetails(startTimer)
        local remaining = remaining / 1000
        outputChatBox(plantedBombs) ---- Should now print the updated value
        dxDrawText("Delivered Bombs : "..plantedBombs.."", 1162, 654, screenW, 711, white, 1.3, "default-bold", "right", "bottom")
        dxDrawText("Time Remaining : "..math.floor(remaining / 60)..":"..math.floor(remaining % 60).."", 1228, 725, screenW, 740, white, 1.3, "default-bold", "right", "bottom")
    end
end

 

Link to comment
2 hours ago, FLUSHBICEPS said:
--serverside
function onHit(player)
    if (#m > 0 and #b > 0) then
        destroyElement(m[1])
        destroyElement(b[1])
        table.remove(m, 1)
        table.remove(b, 1)
    end
    for i, k in ipairs(Bomb2) do
        if (isElementAttached(Bomb2[i])) then
            detachElements(Bomb2[i], helic[1])
            plantedBombs = plantedBombs + 1
            outputChatBox("Bomb has been successfully installed, Do the same with the rest!", root, 34, 139, 34)
            outputChatBox(plantedBombs) ---- This returns what it should return
            triggerClientEvent(player, "updatePlantedBombs", root, plantedBombs)
        end
    end
    if (plantedBombs == 5) then
        triggerEvent("stopMission", root, onHit)
        givePlayerMoney(player, 50000)
        outputChatBox("Congratulations, you have passed the mission and got $50,000 as a reward!", root, 34, 139, 34)
    end
end
--clientside
addEvent("updatePlantedBombs", true)
addEventHandler("updatePlantedBombs", root, function(plantedBombsFromServer)
    plantedBombs = plantedBombsFromServer
end)

function renderTimer()
    local white = tocolor(255, 255, 255, 255)
    if (isTimer(startTimer)) then
        local remaining = getTimerDetails(startTimer)
        local remaining = remaining / 1000
        outputChatBox(plantedBombs) ---- Should now print the updated value
        dxDrawText("Delivered Bombs : "..plantedBombs.."", 1162, 654, screenW, 711, white, 1.3, "default-bold", "right", "bottom")
        dxDrawText("Time Remaining : "..math.floor(remaining / 60)..":"..math.floor(remaining % 60).."", 1228, 725, screenW, 740, white, 1.3, "default-bold", "right", "bottom")
    end
end

 

trigging in the for is not the most appropriate, the best would be outside the for

    for i, k in ipairs(Bomb2) do
        if (isElementAttached(Bomb2[i])) then
            detachElements(Bomb2[i], helic[1])
            plantedBombs = plantedBombs + 1
            outputChatBox("Bomb has been successfully installed, Do the same with the rest!", root, 34, 139, 34)
            outputChatBox(plantedBombs) ---- This returns what it should return
        end
    end
    triggerClientEvent(player, "updatePlantedBombs", root, plantedBombs)

 

  • Like 1
Link to comment

Hello thank you guys for assisting me, it's now working :D

Sorry for being a bit inquirer. I'm facing another thing, i did find a solution for it tho but it's not very good. Lemme show you 

 

local helic = {}

function startM()
	if (not isElement(source) and getElementType(source) == "player") then
		return false
	end
	if (not isPedInVehicle(source)) then
		return false
	end
	local heli = createVehicle(501, 2625.53247, 733.39642, 10.82031)
	table.insert(helic, heli)
	warpPedIntoVehicle(source, heli)
	setElementAlpha(source, 0)
	bindKey(source, "h", "down", attachBomb)
	for i, k in ipairs(Bombs) do
		Bomb1[i] = createObject(1654, Bombs[i][1], Bombs[i][2], Bombs[i][3] - 0.5)
		Bomb2[i] = Bomb1[i]
		setElementCollisionsEnabled(Bomb1[i], false)
	end
	addEventHandler("onVehicleExit", heli, onExit)
	---triggerClientEvent(source, "clientPed", source, startM)
end
addEvent("startMission", true)
addEventHandler("startMission", root, startM)


function onHit(player)
    if (#m > 0 and #b > 0) then
        destroyElement(m[1])
        destroyElement(b[1])
        table.remove(m, 1)
        table.remove(b, 1)
    end
	if (not helic[1]) then
		return false
	end
	local lmnt = getAttachedElements(helic[1])
	if (not lmnt) then
		return false
	end
	for i, k in ipairs(lmnt) do
		if (getElementType(lmnt[i]) == "object" and isElementAttached(lmnt[i])) then
            detachElements(lmnt[i], helic[1])
            plantedBombs = plantedBombs + 1
			if (plantedBombs == 1) then
				outputChatBox("Congratulations, you have passed the mission and got $50,000 as a reward!", root, 34, 139, 34)
			else
				outputChatBox("Bomb has been successfully installed, Do the same with the rest!", root, 34, 139, 34)
				outputChatBox(plantedBombs)
			end
        end
    end
	triggerClientEvent(player, "updatePlantedBombs", root, plantedBombs)
	if (plantedBombs == 1) then
        triggerEvent("stopMission", root, onHit)
		triggerClientEvent(resourceRoot, "stopM", resourceRoot, onHit)
    end
end

I've created a vehicle inside a function, if i knew from the start how to use the variable in other functions when it's inside a function, i wouldn't add it inside a table, i wanna use sometimes the vehicle is client but sadly i couldn't. Can anyone tell me what should i exactly do (Please dont tell me to put the variable global, as i dont want to cause setting it as global will create the vehicle once when the resource start and i dont wanna that)

 

Also, the getAttachedElements(helic[1]) is sending a warning saying that it requires an argument. i guess it's because i'm using helic[1] as an argument. 

Help me, please. i'm new into lua and i wanna create my first working script

Edited by lilwayn
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...