Jump to content

[HELP] Having trouble with "else/if"


Crook4Money

Recommended Posts

I've been trying to make a functioning pay & spray, but have finally came to a problem trying to figure out how to use the else/if. Basically I've gotten the pay and spray to function the way I want it to, but I can't figure out how to keep it from taking money when you don't have enough and how to make it also not fix the vehicle when you don't have enough money. 

local door1 = createObject(5422, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
local marker1 = createMarker(2063.5, -1831.1999511719, 13, "cylinder",6,225,225,255,20)
local isMoving = false
local moveTime = 1000
local doorDown = 5000
local playerMoney = false

function repairVehicle ( thePlayer, commandName) 
    setTimer ( function()
	if ( isPedInVehicle ( thePlayer ) ) then 
        local theVehicle = getPedOccupiedVehicle ( thePlayer) 
        local fix = fixVehicle ( theVehicle ) 
        if ( fix ) then 
			takePlayerMoney(thePlayer, 200)
            outputChatBox (" Your vehicle is restored. ", thePlayer, 0, 200, 0 )
		local money = getPlayerMoney(thePlayer)
		if (money < 200) then
		return false
		end
			elseif
            outputChatBox ( "Failed to fix your vehicle.", thePlayer) then
			givePlayerMoney(thePlayer, 200)
        end 
    else 
        outputChatBox ( "You're not in a vehicle!", thePlayer, 200, 0, 0 )  
	end
end, 5000, 1)
end
addEventHandler("onMarkerHit", root, repairVehicle)

function moveDoor(player)
	if isMoving then return end
	if source == marker1 then
		local move = moveObject(door1, moveTime, 2071.5, -1831.4000244141, 14.60000038147, 0, 90, 0)
		if move then 
			isMoving = false
			setTimer(moveDoor, moveTime, 1, true)
		end
	end
end
addEventHandler("onMarkerHit", root, moveDoor)

function door2()
	setTimer(function() 
	moveObject(door1, moveTime, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
		isMoving = false
end, 6000, 1)
end 
addEventHandler("onMarkerHit", root, door2)

 

Link to comment
Quote

function repairVehicle ( thePlayer, commandName)

local repairMarker = createMarker(2063.5, -1831.1999511719, 13, "cylinder", 2, 255, 0 ,0 ,150)
local repairSecond = 5

local fixTimer = {}
addEventHandler("onMarkerHit", root, function (player) 
    if (source == repairMarker and getElementType(player) == "player") then 
        
        if isPedInVehicle(player) then  
            local vehicle = getPedOccupiedVehicle(player)
            if getElementHealth(vehicle) < 1000 then
        
                if getPlayerMoney(player) < 200 then return outputChatBox("You dont have enough money!", player) end
            
            
                setElementFrozen(vehicle, true)        
            
                fixTimer[player] = setTimer(function()

             
                    fixVehicle(player)
                    outputChatBox("Your vehicle is fixed", player)    
                    setElementFrozen(vehicle, false)        
                end,1000*repairSecond,1)
            else
                outputChatBox("Your vehicle not damaged.", player)                
            end
        end        
    end 
end) 

 

Edited by TheMOG
Link to comment

I just realized that I had the lines in the wrong places. Money should've been first and if there wasn't enough money then it shouldn't fix the vehicle. Crazy it took me several hours to figure that out. lol

I like how you set the vehicle damage to not fix a repaired vehicle. Trying to work that into my script now.

Edited by Crook4Money
Link to comment

Looks like this now.

local door1 = createObject(5422, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
local marker1 = createMarker(2063.5, -1831.1999511719, 13, "cylinder",6,225,225,255,20)
local isMoving = false
local moveTime = 1000
local doorDown = 5000

function repairVehicle ( thePlayer, commandName) 
    setTimer ( function()
	if ( isPedInVehicle ( thePlayer ) ) then 	
        if ( money >= 200 ) then 
			takePlayerMoney(thePlayer, 200)
            outputChatBox (" Your vehicle is restored. ", thePlayer, 0, 200, 0 )
		local theVehicle = getPedOccupiedVehicle ( thePlayer) 
        local fix = fixVehicle ( theVehicle )
		local money = getPlayerMoney(thePlayer)	
		if (money < 200) then 
		fixVehicle ( theVehicle ) return false
		end
			else
            outputChatBox ( "Failed to fix your vehicle.", thePlayer) 
        end 
    else 
        outputChatBox ( "You're not in a vehicle!", thePlayer, 200, 0, 0 )  
	end
end, 5000, 1)
end
addEventHandler("onMarkerHit", root, repairVehicle)

function moveDoor(player)
	if isMoving then return end
	if source == marker1 then
		local move = moveObject(door1, moveTime, 2071.5, -1831.4000244141, 14.60000038147, 0, 90, 0)
		if move then 
			isMoving = false
			setTimer(moveDoor, moveTime, 1, true)
		end
	end
end
addEventHandler("onMarkerHit", root, moveDoor)

function door2()
	setTimer(function() 
	moveObject(door1, moveTime, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
		isMoving = false
end, 6000, 1)
end 
addEventHandler("onMarkerHit", root, door2)

 

Edited by Crook4Money
Link to comment

Edited last post too many times. Cut out a bit of the script and messed it up and had to fix it. It's supposed to look like this.

I tried to add "if getElementHealth(vehicle) < 1000 then"

but I couldn't get it to output "Vehicle is already repaired." using else/ifs. I just got a botched script if I added element data to get that output. Otherwise it told me I wasn't in a vehicle when I was. I'll try to recreate that script. I just wanted to post this here before I broke the script again.

local door1 = createObject(5422, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
local marker1 = createMarker(2063.5, -1831.1999511719, 13, "cylinder",6,225,225,255,20)
local isMoving = false
local moveTime = 1000
local doorDown = 5000

function repairVehicle ( thePlayer, commandName) 
    setTimer ( function()
	if ( isPedInVehicle ( thePlayer ) ) then 
		local vehicle = getPedOccupiedVehicle(player)
		local money = getPlayerMoney(thePlayer)	
        if ( money >= 200 ) then 
			takePlayerMoney(thePlayer, 200)
            outputChatBox (" Your vehicle is restored. ", thePlayer, 0, 200, 0 )
		local theVehicle = getPedOccupiedVehicle ( thePlayer) 
        local fix = fixVehicle ( theVehicle )
		if (money < 200) then 
		fixVehicle ( theVehicle ) return false
		end
			else
            outputChatBox ( "Failed to fix your vehicle.", thePlayer) 
        end 
    else 
        outputChatBox ( "You're not in a vehicle!", thePlayer, 200, 0, 0 )  
	end
end, 5000, 1)
end
addEventHandler("onMarkerHit", root, repairVehicle)

function moveDoor(player)
	if isMoving then return end
	if source == marker1 then
		local move = moveObject(door1, moveTime, 2071.5, -1831.4000244141, 14.60000038147, 0, 90, 0)
		if move then 
			isMoving = false
			setTimer(moveDoor, moveTime, 1, true)
		end
	end
end
addEventHandler("onMarkerHit", root, moveDoor)

function door2()
	setTimer(function() 
	moveObject(door1, moveTime, 2072.5, -1831.4200244141, 16.5, 0, -90, 0)
		isMoving = false
end, 6000, 1)
end 
addEventHandler("onMarkerHit", root, door2)

 

Edited by Crook4Money
Link to comment

First thing first, onMarkerHit needs marker element instead of root element as second argument if you want to start the function only in certain marker, check it there:
https://wiki.multitheftauto.com/wiki/OnMarkerHit
 

And about the code, this should work, i haven't tested it, but you can see how it could be done:

local marker1 = createMarker(2063.5, -1831.1999511719, 13, "cylinder",6,225,225,255,20)
function repairVehicle ( thePlayer, matchingDimension) 
    setTimer ( function()
    	if thePlayer ~= localPlayer then return end

    	local theVehicle = getPedOccupiedVehicle( thePlayer )
    	if not theVehicle then
    		return outputChatBox( "You're not in a vehicle!", thePlayer, 200, 0, 0 )
    	end

    	local vehHealth = getElementHealth( theVehicle )
    	if vehHealth >= 999 then
    		return outputChatBox( "Your vehicle isn't damaged!", thePlayer, 200, 0, 0 )
    	end

    	local money = getPlayerMoney( thePlayer )
    	if money >= 200 then
    		local fixVeh = fixVehicle(  theVehicle )
    		if fixVeh then
    			takePlayerMoney( thePlayer, 200 )
    			return outputChatBox ("Your vehicle is restored.", thePlayer, 0, 200, 0 )
    		else
    			return outputChatBox ( "Failed to fix your vehicle.", thePlayer) 
    		end
    	end
	end, 5000, 1)
end
addEventHandler("onMarkerHit", marker1, repairVehicle)

 

  • Thanks 1
Link to comment

Ah, thanks. I was curious if other markers would interact the same way. I was just using root as a placeholder for not knowing what I'm doing. ;)

I'll try to implement this tomorrow. I like the simplicity of it anyways and doesn't seem to cause as many issues as I was having earlier.

Edited by Crook4Money
Link to comment

That was kind've the problem I guess. I copied one of your guys scripts that had the argument like this.

local vehHealth = getElementHealth( theVehicle )

I had to change all of "theVehicle"s to just "vehicle". I finally got my script working as it should now with a ton of trying to do workarounds till I figured out why the element wasn't established. I'm not sure in which cases you'd use "theVehicle", but I know that it works cos I tried MOG's script and it was functional in his. Maybe I'm just missing something with that. Idk. Was funny tho cos I actually rebuilt my script in a different structure and tho it was pointless what I needed it to do came out better and more consistent. Probably.

Only messing around with scripting for a month or two is pretty tough especially just getting into creating my own script for once and getting into the use of else/if. I definitely wouldn't have been able to get my script to function the way I wanted to without your guys help. Thanks ;)

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