Jump to content

[Question] setElementModel and getPlayerMoney


kieran

Recommended Posts

I made a gui for a skin shop, basically it is meant to check number from gridlist column and then use that for setElementModel, problem is that when I click it....

it gives this warning: WARNING:GUI's/clothes_c.lua:28: Bad argument @ 'setElementModel'  [Expected element at argument 1, got string '1']

Spoiler

ClothMark = createMarker (7139.2646484375, -4865.3818359375, 9.2931632995605,"cylinder", 1.1, 0, 255, 0)

window = {}

function SkinShop()
        window = guiCreateWindow(0.74, 0.22, 0.25, 0.72, "GTA_3 Skin Shop", true)
        guiWindowSetSizable(window, false)

        gridlist = guiCreateGridList(0.06, 0.06, 0.89, 0.86, true, window)
        guiGridListAddColumn(gridlist, "Name", 0.3)
        guiGridListAddColumn(gridlist, "ID", 0.3)
        guiGridListAddColumn(gridlist, "Price", 0.3)
        guiGridListAddRow(gridlist)
        guiGridListSetItemText(gridlist, 0, 1, "CJ", false, false)
        guiGridListSetItemText(gridlist, 0, 2, "1", false, false)
        guiGridListSetItemText(gridlist, 0, 3, "500", false, false)
        button1 = guiCreateButton(0.05, 0.94, 0.41, 0.03, "Buy", true, window)
        button2 = guiCreateButton(0.54, 0.94, 0.41, 0.03, "Exit", true, window) 
	addEventHandler ( "onClientGUIClick", gridlist, click )		
end
addEventHandler ("onClientMarkerHit", ClothMark, SkinShop)




function click () 
       local SkinModel = guiGridListGetItemText ( gridlist, guiGridListGetSelectedItem ( gridlist ), 2 )
       setElementModel (SkinModel)
	   
	   local SkinCost = guiGridListGetItemText ( gridlist, guiGridListGetSelectedItem ( gridlist ), 3 )
       takePlayerMoney (SkinCost)
end

addEvent( "ClothLoad", true )
addEventHandler( "ClothLoad", resourceRoot, SkinShop )

 

I also want to check players money, so I changed 

Spoiler

function click () 
       local SkinModel = guiGridListGetItemText ( gridlist, guiGridListGetSelectedItem ( gridlist ), 2 )
       setElementModel (SkinModel)
	   
	   local SkinCost = guiGridListGetItemText ( gridlist, guiGridListGetSelectedItem ( gridlist ), 3 )
       takePlayerMoney (SkinCost)
end

 

To 

Spoiler

function click () 
       local SkinModel = guiGridListGetItemText ( gridlist, guiGridListGetSelectedItem ( gridlist ), 2 )
       setElementModel (SkinModel)
	   
	   players_money = getPlayerMoney
	   local SkinCost = guiGridListGetItemText ( gridlist, guiGridListGetSelectedItem ( gridlist ), 3 )
	   if ( tonumber ( players_money ) >= tonumber (SkinCost)) then
       takePlayerMoney (SkinCost)
		else
	   outputChatBox("You do not have enough money.")
end
end

 

But it then gave this error, ERROR: GUI's/clothes_c.lua:32: attempt to compare number with nil (Line 32 would be line 7)

Thanks for any help!

Edited by kieran
Link to comment

First of all, line 6 of the last snippet is wrong

players_money = getPlayerMoney -- copies function getPlayerMoney to players_money variable

-- instead, it should be
players_money = getPlayerMoney() -- copies the RETURN from the function call

Secondly, I strongly advise against using takePlayerMoney() on the clientside since it won't sync with the server (as to prevent clientside money hacks)

Thirdly, 

setElementModel (SkinModel) -- is missing the first parameter - the element which you want to change the model of

-- properly, it should be
setElementModel (localPlayer, SkinModel) -- also, note that this will change the model only on the client's side - it won't be synced to the server nor any other players

 

Edited by MrTasty
  • Like 1
Link to comment

I had the exact same idea @koragg, decided just to get the text, then check if it's a number, and finally....  It works! :D now I just gotta make the server side stuff to set skins etc... :) Thanks for help, and if anyone is getting error with tonumber saying ) expected near =, then try == :D 

 

Link to comment
1 hour ago, TheMOG said:

if you using setElementModel on client side, other players not seeing your new elementModel

I'm using triggerServerEvent to trigger it server side....  In fact there is a problem, it won't do anything :/ so instead of making new topic I'm just gonna be a d**k and revive this xD

Server 

Spoiler

function MySkin_1 (thePlayer)
	setElementModel ( thePlayer, 280 )
end
addEvent( "MySkin1", true )
addEventHandler( "MySkin1", resourceRoot, MySkin_1 ) 

addCommandHandler ( "changeveh",
    function ( thePlayer, command, newModel )
        local theVehicle = getPedOccupiedVehicle ( thePlayer ) -- get the vehicle the player is in
        newModel = tonumber ( newModel )                          -- try to convert the string argument to a number
        if theVehicle and newModel then                           -- make sure the player is in a vehicle and specified a number
            setElementModel ( theVehicle, newModel )
        end
    end
)

 

 Client

Spoiler

function click (thePlayer) 
       local SkinModel = guiGridListGetItemText ( gridlist, guiGridListGetSelectedItem ( gridlist ), 2 )
       if ( tonumber (SkinModel) == tonumber ( 1 ) ) then
	   triggerServerEvent ( "MySkin1", resourceRoot )
	   elseif ( tonumber (SkinModel) == tonumber ( 2 ) ) then
	   outputChatBox("Skin 2")
end
end

 

Basically, it triggers the event, but it says it expects element at first argument of setElementModel (Don't worry about the elseif, that was me checking it to see if it registered mouse click and it did). 

Anyone know what I'm doing wrong? xD 

Noticed there isn't a simple easy to read skin shop script on community, so this is basically the main purpose of this script...  (Later all skin names, etc will be added)

Link to comment

I seen so many times in your code that you just assume thePlayer is like a magic keyword, whenever you use it without it being defined it would just work and get the player. That is not how it works, there are "magic" variables like source, client, resourceRoot, root etc which you don't have to define, but thePlayer is not always a parameter... 

 

When you trigger from client to server and the sendTo argument (2nd arg.) is the localPlayer, you can use the variable client without defining it. Change thePlayer @line 3 to client on s side and @line 5 resourceRoot to localPlayer c side.

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