Jump to content

I am new to Mtasa scripting. help


scolen

Recommended Posts

If a player hits this marker, every player on the same server will start working. What I want to do is to make it work only for the player who hits the marker. How to fix this?

getJobMarker = createMarker(872.03754, -29.07278, 62.3000, "cylinder", 1.0, 0, 255, 255, 255, getRootElement())
ped = createPed(159, 872.92389, -27.08743, 63.94565, 160)
setPedFrozen(ped, true)
blip = createBlipAttachedTo(getJobMarker, 56)

function hitMarkerWindow()
    window = guiCreateWindow(0.35, 0.35, 0.35, 0.30, "wood job", true)
    memo = guiCreateMemo(0.10, 0.20, 0.80, 0.4, "this is wood cutting job", true, window)
    accept = guiCreateButton(0.10, 0.75, 0.22, 0.14, "Accept Job", true, window)
    leave = guiCreateButton(0.35, 0.75, 0.22, 0.14, "Leave Job", true, window)
    close = guiCreateButton(0.60, 0.75, 0.22, 0.14, "Close", true, window)
    showCursor(true)
    guiMemoSetReadOnly(memo, true)
end

addEventHandler("onClientMarkerHit", getJobMarker, hitMarkerWindow)

function clientClick()
    if source == close then 
        guiSetVisible(window, false)
        showCursor(false)
        triggerServerEvent("cancelButton", resourceRoot, localPlayer)
    elseif source == accept then
        guiSetVisible(window, false)
        showCursor(false)
        triggerServerEvent("acceptWoodJob", resourceRoot, localPlayer)
    elseif source == leave then 
        guiSetVisible(window, false)
        showCursor(false)
        triggerServerEvent("leaveJob", resourceRoot, localPlayer)

    end
end

addEventHandler("onClientGUIClick", root,  clientClick)

 

Link to comment

Hi. So I can see where you've gone wrong. You are not checking which element has hit the marker. You should check this is the localPlayer.

 

function hitMarkerWindow(thePlayer, matchingDimension)
	if thePlayer and thePlayer == localPlayer and matchingDimension then -- check the player is the localplayer, also check matching dimension to be 100% sure you're in the right place.
      window = guiCreateWindow(0.35, 0.35, 0.35, 0.30, "wood job", true)
      memo = guiCreateMemo(0.10, 0.20, 0.80, 0.4, "this is wood cutting job", true, window)
      accept = guiCreateButton(0.10, 0.75, 0.22, 0.14, "Accept Job", true, window)
      leave = guiCreateButton(0.35, 0.75, 0.22, 0.14, "Leave Job", true, window)
      close = guiCreateButton(0.60, 0.75, 0.22, 0.14, "Close", true, window)
      showCursor(true)
      guiMemoSetReadOnly(memo, true)
	end
end
addEventHandler("onClientMarkerHit", getJobMarker, hitMarkerWindow)


Hope this helps.


- TM

  • Thanks 1
Link to comment
20 hours ago, FLUSHBICEPS said:

Show your server side script


    outputChatBox("Wood cutting job start", source, 0,255,255)


local table = {
    {866.98505, -37.24827, 63.04229},
    {890.52130, -37.67257, 63.19531},
    {889.49915, -47.77541, 62.52142},
    {903.36798, -48.64642, 64.13943},
    {849.61884, -17.29530, 64.28951},
    {842.59650, -30.73505, 62.30632}
}

getElementData()
function startJob(thePlayer)

   if not (getElementData(thePlayer, "Jobs") == "WoodMan") then 
        
        setElementData(thePlayer, "Jobs", "WoodMan")
        woodCut(thePlayer)
   else
    outputChatBox("you are alrady Woodman", thePlayer, 0, 255,255)
    
   end

end


function woodCut(thePlayer)
    local one = math.random(#table)
    local x = table [one][1]
    local y = table [one][2]
    local z = table [one][3]
    marker = createMarker(x,y,z-1, "cylinder", 2.0, 0, 255, 0)
    blip = createBlipAttachedTo(marker, 22)
    
    setElementVisibleTo(blip, getRootElement(), false)
    setElementVisibleTo(blip, thePlayer, true)

    setElementVisibleTo(marker, getRootElement(), false)
    setElementVisibleTo(marker, thePlayer, true)
    outputChatBox("now Wood Man"..x..y..z, thePlayer, 0, 255,255)
    check()
end

function check()

    addEventHandler("onMarkerHit", marker, 
       function(thePlayer)
        outputChatBox("Hitted", thePlayer, 0,255,255)
        destroyElement(marker)
        destroyElement(blip)
        woodCut(thePlayer)

       
       end
    )

end


function cancelJob(thePlayer)
    setPlayerSkin(thePlayer, 210)
end
function leaveJob(thePlayer)
    setElementData(thePlayer, "Jobs", nil)
    destroyElement(marker)
    destroyElement(blip)
end

addEvent("acceptWoodJob", true)
addEventHandler("acceptWoodJob", root, startJob)

addEvent("leaveJob", true)
addEventHandler("leaveJob", root, leaveJob)

addEvent("cancelButton", true)
addEventHandler("cancelButton", root, cancelJob)

 

20 hours ago, FLUSHBICEPS said:

Show your server side script

 

Link to comment

server side

function woodCut(thePlayer)
    local one = math.random(#table)
    local x = table[one][1]
    local y = table[one][2]
    local z = table[one][3]
    marker = createMarker(x, y, z-1, "cylinder", 2.0, 0, 255, 0)
    blip = createBlipAttachedTo(marker, 22)
    
    setElementVisibleTo(blip, getRootElement(), false)
    setElementVisibleTo(blip, thePlayer, true)

    setElementVisibleTo(marker, getRootElement(), false)
    setElementVisibleTo(marker, thePlayer, true)
    outputChatBox("now Wood Man"..x..y..z, thePlayer, 0, 255, 255)
    check(thePlayer)
end

function check(thePlayer)
    addEventHandler("onMarkerHit", marker, 
       function(hitElement, matchingDimension)
           if hitElement == thePlayer and matchingDimension then
               outputChatBox("Hitted", hitElement, 0, 255, 255)
               destroyElement(marker)
               destroyElement(blip)
               woodCut(thePlayer)
           end
       end
    )
end

check() func ain’t taking the player who triggered the event into account also Ive added a few checks which ensures that whoever hits the marker will start working, which is ur issue

client side

function hitMarkerWindow(hitElement, matchingDimension)
    if hitElement == localPlayer and matchingDimension then
        window = guiCreateWindow(0.35, 0.35, 0.35, 0.30, "wood job", true)
        memo = guiCreateMemo(0.10, 0.20, 0.80, 0.4, "this is wood cutting job", true, window)
        accept = guiCreateButton(0.10, 0.75, 0.22, 0.14, "Accept Job", true, window)
        leave = guiCreateButton(0.35, 0.75, 0.22, 0.14, "Leave Job", true, window)
        close = guiCreateButton(0.60, 0.75, 0.22, 0.14, "Close", true, window)
        showCursor(true)
        guiMemoSetReadOnly(memo, true)
    end
end

addEventHandler("onClientMarkerHit", getJobMarker, hitMarkerWindow)

 

Link to comment
13 hours ago, FLUSHBICEPS said:

server side

function woodCut(thePlayer)
    local one = math.random(#table)
    local x = table[one][1]
    local y = table[one][2]
    local z = table[one][3]
    marker = createMarker(x, y, z-1, "cylinder", 2.0, 0, 255, 0)
    blip = createBlipAttachedTo(marker, 22)
    
    setElementVisibleTo(blip, getRootElement(), false)
    setElementVisibleTo(blip, thePlayer, true)

    setElementVisibleTo(marker, getRootElement(), false)
    setElementVisibleTo(marker, thePlayer, true)
    outputChatBox("now Wood Man"..x..y..z, thePlayer, 0, 255, 255)
    check(thePlayer)
end

function check(thePlayer)
    addEventHandler("onMarkerHit", marker, 
       function(hitElement, matchingDimension)
           if hitElement == thePlayer and matchingDimension then
               outputChatBox("Hitted", hitElement, 0, 255, 255)
               destroyElement(marker)
               destroyElement(blip)
               woodCut(thePlayer)
           end
       end
    )
end

check() func ain’t taking the player who triggered the event into account also Ive added a few checks which ensures that whoever hits the marker will start working, which is ur issue

client side

function hitMarkerWindow(hitElement, matchingDimension)
    if hitElement == localPlayer and matchingDimension then
        window = guiCreateWindow(0.35, 0.35, 0.35, 0.30, "wood job", true)
        memo = guiCreateMemo(0.10, 0.20, 0.80, 0.4, "this is wood cutting job", true, window)
        accept = guiCreateButton(0.10, 0.75, 0.22, 0.14, "Accept Job", true, window)
        leave = guiCreateButton(0.35, 0.75, 0.22, 0.14, "Leave Job", true, window)
        close = guiCreateButton(0.60, 0.75, 0.22, 0.14, "Close", true, window)
        showCursor(true)
        guiMemoSetReadOnly(memo, true)
    end
end

addEventHandler("onClientMarkerHit", getJobMarker, hitMarkerWindow)

 

There is another problem. I made this wood cut job. When a marker is hit, that marker is destroyed and randomly created from another place. But after I accept the job together with my friend, when my friend hits a marker, the marker shown to me is destroyed. How can I fix it?

Link to comment

u will need to create separate marker and blip for each player 

local markers = {}
local blips = {}

function woodCut(thePlayer)
    local one = math.random(#table)
    local x = table[one][1]
    local y = table[one][2]
    local z = table[one][3]
    local marker = createMarker(x, y, z-1, "cylinder", 2.0, 0, 255, 0)
    local blip = createBlipAttachedTo(marker, 22)
    
    markers[thePlayer] = marker
    blips[thePlayer] = blip

    setElementVisibleTo(blip, getRootElement(), false)
    setElementVisibleTo(blip, thePlayer, true)

    setElementVisibleTo(marker, getRootElement(), false)
    setElementVisibleTo(marker, thePlayer, true)
    outputChatBox("now Wood Man"..x..y..z, thePlayer, 0, 255, 255)
    check(thePlayer)
end

function check(thePlayer)
    local marker = markers[thePlayer]
    addEventHandler("onMarkerHit", marker, 
       function(hitElement, matchingDimension)
           if hitElement == thePlayer and matchingDimension then
               outputChatBox("Hitted", hitElement, 0, 255, 255)
               destroyElement(marker)
               destroyElement(blips[thePlayer])
               woodCut(thePlayer)
           end
       end
    )
end

I’ve added two tables to store players markers and blips and made a few changes to both funcs

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