Jump to content

Rainbow


Dimos7

Recommended Posts

function rainbowColor(thePlayer) 
    if isPedInVehicle(thePlayer) then 
       if getPedOccupiedVehicleSeat(thePlayer, 0) then
        local theVehicle = getPedOccupiedVehicle(thePlayer) 
         
        local rainbowTimer = getElementData(theVehicle, "rainbowTimer") 
        if isTimer(rainbowTimer) then 
            killTimer(rainbowTimer)
            return 
        end 
         
        local money = getPlayerMoney(thePlayer) 
        if money >= 10000 then 
            takePlayerMoney(thePlayer, 10000) 
            local rainbowTimer = setTimer(function (veh) 
                local r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255) 
                setVehicleColor(veh, r, g, b) 
            end, 1000, 0, theVehicle) 
            setElementData(theVehicle, "rainbowTimer", rainbowTimer, false) 
        else 
            outputChatBox("You do not have enough money for this ! ($10000 required)", thePlayer, 255, 0, 0) 
        end 
      else
         outputChatBox("You must be at driver seat for turn on/off!", thePlayer, 255, 0 ,0)
      end
    else 
        outputChatBox("You have to be in a vehicle to do that !", thePlayer, 255, 0, 0) 
    end 
end 
addCommandHandler("rainbow", rainbowColor) 

I use this code for race gamemode and every map need do /rainbow can help me

Edited by Dimos7
Link to comment

Your best bet would be to use it server-side and use the event onResourceStart to run it whenever a resource is started. As far as I know there's no actual way of checking whether the resource is a map or not(well, I guess you can check if a file with the .map extension exists either via fileExists or reading the meta via XML functions).

Another workaround would be to make a table containing names of resources to ignore, so the script isn't run if for example you restart a resource that is not a map.

Link to comment
  • 4 weeks later...
function rainbowColor(thePlayer) 
    if isPedInVehicle(thePlayer) then 
       if getPedOccupiedVehicleSeat(thePlayer, 0) then
        local theVehicle = getPedOccupiedVehicle(thePlayer) 
         
        local rainbowTimer = getElementData(theVehicle, "rainbowTimer") 
        if isTimer(rainbowTimer) then 
            killTimer(rainbowTimer)
            return 
        end 
         
        local money = getPlayerMoney(thePlayer) 
        if money >= 10000 then 
            takePlayerMoney(thePlayer, 10000) 
            local rainbowTimer = setTimer(function (veh) 
                local r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255) 
                setVehicleColor(veh, r, g, b) 
            end, 500, 0, theVehicle) 
            setElementData(theVehicle, "rainbowTimer", rainbowTimer, false) 
        else 
            outputChatBox("You do not have enough money for this ! ($10000 required)", thePlayer, 255, 0, 0) 
        end 
      else
         outputChatBox("You must be at driver seat for turn on/off!", thePlayer, 255, 0 ,0)
      end
    else 
        outputChatBox("You have to be in a vehicle to do that !", thePlayer, 255, 0, 0) 
    end 
end 
addCommandHandler("rainbow", rainbowColor)

addEventHandler("onResourceStart", resourceRoot, 
   function(map)
      if fileExists(map) and isTimer(rainbowTimer) then
	     setTimer(rainbowTimer)
	  end
	end)
	
function destoryRain()
    if isTimer(rainbowTimer) then 
	   killTimer(rainbowTimer)
	end
end
addEventHandler("onPlayerQuit", root, destoryRain)

I try it make it like this but seems not work

 

Edited by Dimos7
Link to comment
1 hour ago, Dimos7 said:

onResourceStart's first argument is the resource that started. So in this case, you're defining the resource as 'map' and then checking if the 'map' aka started resource exists. FileExists's first argument is the string representing the file you want to check if exists. Hope you understand :)

Link to comment
function rainbowColor(thePlayer) 
    if isPedInVehicle(thePlayer) then 
       if getPedOccupiedVehicleSeat(thePlayer, 0) then
        local theVehicle = getPedOccupiedVehicle(thePlayer) 
         
        local rainbowTimer = getElementData(theVehicle, "rainbowTimer") 
        if isTimer(rainbowTimer) then 
            killTimer(rainbowTimer)
            return 
        end 
         
        local money = getPlayerMoney(thePlayer) 
        if money >= 10000 then 
            takePlayerMoney(thePlayer, 10000) 
            local rainbowTimer = setTimer(function (veh) 
                local r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255) 
                setVehicleColor(veh, r, g, b) 
            end, 500, 0, theVehicle) 
            setElementData(theVehicle, "rainbowTimer", rainbowTimer, false) 
        else 
            outputChatBox("You do not have enough money for this ! ($10000 required)", thePlayer, 255, 0, 0) 
        end 
      else
         outputChatBox("You must be at driver seat for turn on/off!", thePlayer, 255, 0 ,0)
      end
    else 
        outputChatBox("You have to be in a vehicle to do that !", thePlayer, 255, 0, 0) 
    end 
end 
addCommandHandler("rainbow", rainbowColor)

addEventHandler("onResourceStart", resourceRoot, 
   function(map, theVehicle)
      local rainbowTimer = getElementData(theVehicle, "rainbowTimer")
      if map and isTimer(rainbowTimer) then
	    setTimer(rainbowTimer)
	  end
	end)
	
function destoryRain(theVehicle)
    local rainbowTimer = getElementData(theVehicle, "rainbowTimer")
    if isTimer(rainbowTimer) then 
	   killTimer(rainbowTimer)
	end
end
addEventHandler("onPlayerQuit", root, destoryRain)

you mean something like that?

Edited by Dimos7
Link to comment

 

8 hours ago, Dimos7 said:

In both these functions, there is no rainbowTimer defined. Also, when you're checking 'if map' then this means that you're checking if the started resource started, that's obvious, it will return true in all cases. What are you actually trying to do? If you want to kill timers attached to some vehicles, you can use it via a table instead. Something like this:

timers = {}

function addTimer(player, cmd)
	local vehicle = getPedOccupiedVehicle(player)
	if vehicle then
		local timers[vehicle] = setTimer(theFunction, ms, timesToExecute)
		setElementData(vehicle, "rainbowTimer", timers[vehicle])
	end
end
addCommandHandler("commanad", addTimer)

function removeTimer()
	local vehicle = getPedOccupiedVehicle(source)
	if vehicle then
		if getElementData(vehicle, "rainbowTimer") then
			setElementData(vehicle, "rainbowTimer", false)
			killTimer(timers[vehicle])
		end
	end
end
addEventHandler("onPlayerQuit", root, removeTimer)

 

Link to comment
  • 1 month later...
local wr, wg, wb = 1, 1, 1
local curframemete = 2
local wrd , wgd, wbd = false, false, false

local setElementData(localPlayer, "rainbowenabled", 0)

function createRainbow()
    for i, player in ipairs(getElementsByType("player")) do
	    if (wr > 0 and wrd == false) then
            wr = wr+curframemete
		   if (wb ~= 1) then
		       wb = wb-curframemete
		   end
		   if (wr > 254) then
		       wr = 255
			   wrd = true
		   end
		elseif (wg > 0 and wgd == false) then
		    wg = wg+curframemete
			wr = wr-curframemete
			if (wg >254) then
			    wg = 255
				wgd = true
		    end
		elseif (wb >0 and wbd == false) then
		    wb = wb+curframemete
			wg = wg-curframemete
			if (wb > 254) then
			    wbd = false
				wrd = false
				wgd = false
			    wb = 255
				wr = 1
				wg = 1
		   end
		end
		   if getPedOccupiedVehicle(player) then
		      if getPedOccupiedVehicleSeat(player) == 0 then
			     if getElementData(player, "rainbowenabled") == 0 then
			        local money = getPlayerMoney()
				    if money > 100000 then
				       takePlayerMoney(100000)
					   setVehicleColor(getPedOccupiedVehicle(player), wr, wg, wb, 0, 0, 0)
					   addEventHandler("onClientRender", root, createRainbow)
					   setElementData(player, "rainbowenabled", 1)
				    else
				       outputChatBox("You do not have enough money for this! ($100000 required)", 255, 0, 0)
			        end
				else
				    setElementData(player, "rainbowenabled", 0)
				    removeEventHandler("onClientRender", root, createRainbow)
			    end
			  else
			     outputChatBox("You must be at driver seat to turn on/off!", 255, 0, 0)
			  end
		   else
 		       outputChatBox("You must be in a vehicle to do that!", 255, 0, 0)
		   end
	end
end
addCommandHandler("rainbow", createRainbow)			

expect ")" near , line 5 but if i will then a end will not be needed at function but it correct

Edited by Dimos7
Link to comment
local wr, wg, wb = 1, 1, 1
local curframemete = 2
local wrd , wgd, wbd = false, false, false

addEventHandler ( 'onResourceStart',root,
function ()
 setElementData(localPlayer, "rainbowenabled", 0)
end)

function createRainbow()
    for i, player in ipairs(getElementsByType("player")) do
        if (wr > 0 and wrd == false) then
            wr = wr+curframemete
           if (wb ~= 1) then
               wb = wb-curframemete
           end
           if (wr > 254) then
               wr = 255
               wrd = true
           end
        elseif (wg > 0 and wgd == false) then
            wg = wg+curframemete
            wr = wr-curframemete
            if (wg >254) then
                wg = 255
                wgd = true
            end
        elseif (wb >0 and wbd == false) then
            wb = wb+curframemete
            wg = wg-curframemete
            if (wb > 254) then
                wbd = false
                wrd = false
                wgd = false
                wb = 255
                wr = 1
                wg = 1
           end
        end
           if getPedOccupiedVehicle(player) then
              if getPedOccupiedVehicleSeat(player) == 0 then
                 if getElementData(player, "rainbowenabled") == 0 then
                    local money = getPlayerMoney()
                    if money > 100000 then
                       takePlayerMoney(100000)
                       setVehicleColor(getPedOccupiedVehicle(player), wr, wg, wb, 0, 0, 0)
                       addEventHandler("onClientRender", root, createRainbow)
                       setElementData(player, "rainbowenabled", 1)
                    else
                       outputChatBox("You do not have enough money for this! ($100000 required)", 255, 0, 0)
                    end
                else
                    setElementData(player, "rainbowenabled", 0)
                    removeEventHandler("onClientRender", root, createRainbow)
                end
              else
                 outputChatBox("You must be at driver seat to turn on/off!", 255, 0, 0)
              end
           else
               outputChatBox("You must be in a vehicle to do that!", 255, 0, 0)
           end
    end
end
addCommandHandler("rainbow", createRainbow)         

try this 

and if you want to start it with a map you can use this

<meta>
<include resource="rainbow" />
</meta>
--or add its .lua file in the folder 
<meta>
<script src="rainbow.lua" type="server"/>
</meta>

 

after this change i have found a bug in this compare in line 44

Link to comment
1 minute ago, Dimos7 said:

its client

uh didn't notice that because i am sleepy -_- coz it's 2 :0 9 am over here

any ways you can make a server.lua

then use

tiggerServerEvent

that would be better in my opinion.

wait actually i have  tested it in-game and it worked but there is a problem.it sets the color to brown and never changes after that.

(After changing it into a client in Meta)

Link to comment
4 hours ago, ViRuZGamiing said:

local wr, wg, wb = 1, 1, 1
local curframemete = 2
local wrd , wgd, wbd = false, false, false

has to be


local wr = 1, wg = 1, wb = 1
local curframemete = 2
local wrd = false, wgd = false, wbd = false

 

Don't mind my C# behaviour lol I was wrong here. Didn't ever look this up for LUA so now I know.

Quote

Lua allows multiple assignment, where a list of values is assigned to a list of variables in one step. Both lists have their elements separated by commas. For instance, in the assignment


    a, b = 10, 2*x

the variable a gets the value 10 and b gets 2*x.

In a multiple assignment, Lua first evaluates all values and only then executes the assignments. Therefore, we can use a multiple assignment to swap two values, as in


    x, y = y, x                -- swap `x' for `y'
    a[i], a[j] = a[j], a[i]    -- swap `a[i]' for `a[j]'

 http://www.lua.org/pil/4.1.html

We learn everyday I guess :) 

  • Like 1
Link to comment
local wr, wg, wb = 1, 1, 1
local curframemete = 2
local wrd , wgd, wbd = false, false, false

setElementData(localPlayer, "rainbowenabled", 0)

function createRainbow()
    for i, player in ipairs(getElementsByType("player")) do
	    if (wr > 0) and (wrd == false) then
            wr = wr+curframemete
		   if (wb ~= 1) then
		       wb = wb-curframemete
		   end
		   if (wr > 254) then
		       wr = 255
			   wrd = true
		   end
		elseif (wg > 0 ) and (wgd == false) then
		    wg = wg+curframemete
			wr = wr-curframemete
			if (wg >254) then
			    wg = 255
				wgd = true
		    end
		elseif (wb > 0) and (wbd == false) then
		    wb = wb+curframemete
			wg = wg-curframemete
			if (wb > 254) then
			    wbd = false
				wrd = false
				wgd = false
			    wb = 255
				wr = 1
				wg = 1
		   end
		end
		   if getPedOccupiedVehicle(player) then
		      if getPedOccupiedVehicleSeat(player) == 0 then
			     if getElementData(player, "rainbowenabled") == 0 then
			        local money = getPlayerMoney()
				    if money >= 100000 then
				       takePlayerMoney(100000)
					   setVehicleColor(getPedOccupiedVehicle(player), wr, wg, wb, 0, 0, 0)
					   addEventHandler("onClientRender", root, createRainbow)
					   setElementData(player, "rainbowenabled", 1)
				    else
				       outputChatBox("You do not have enough money for this! ($100000 required)", 255, 0, 0)
			        end
				else
				    setElementData(player, "rainbowenabled", 0)
				    removeEventHandler("onClientRender", root, createRainbow)
			    end
			  else
			     outputChatBox("You must be at driver seat to turn on/off!", 255, 0, 0)
			  end
		   else
 		       outputChatBox("You must be in a vehicle to do that!", 255, 0, 0)
		   end
	end
end
addCommandHandler("rainbow", createRainbow)

color not change only but a color and after not change

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