Jump to content

Would this work?


Recommended Posts

Ok so I am new to scripting MTA:SA and I have scripted Squirrel, and Pawn, but I was wondering if this would work in LUA for MTA.

playerData = {}
 
function modeName(mode) (
0 = Freeroam,
1 = Roleplay,
2 = Racing
end
)
 
addEventHandler("onPlayerJoin", root,
function()
	playerData = {}
	playerData.mode = 0;
outputChatBox('Welcome to our server', player, 0, 255, 0)
outputChatBox('Your current mode is: '..modeName[playerData.role]..'.', player, 0, 255, 0)
end
)

If it doesn't work how would I go about doing something like that?

Link to comment

well first you might want your modeName function to be a table instead

modeName = {"Freeroam","Roleplay","Racing"} -- modeName[1] would now be "Freeroam" also note the quotes to make sure it's a string

tables in lua usually start with 1 therefore we should turn the 0 to 1, the 1 to 2 and the 2 3 later.

you could also keep it as a function but then you'd need to change it a bit: (I don't really recommend this in this case)

function modeName(mode)  -- you don't need the (
-- make the function return something depending on what "mode" is
if mode == 1 then
return "Freeroam"
elseif mode == 2 then
return "Roleplay"
else
return "Racing"
end
end
-- or the )

now for your onPlayerJoin function:

first you probably don't want to empty the playerData every time a player joins playerData = {} means that playerData is now an empty table, in your example it wouldn't cause any trouble, but you don't need to turn playerData into a table twice.

if you chose the first solution earlier this function should look like this:

addEventHandler("onPlayerJoin", root,
function()
--playerData = {}
     playerData.mode = 1;
outputChatBox('Welcome to our server', source, 0, 255, 0) -- player does not exist here, what you need is source, the source of the event in this case is the player you want, the wiki pages of the different events will always tell you what the source of the events will be
outputChatBox('Your current mode is: '..modeName[playerData.mode]..'.', source, 0, 255, 0) -- not quite sure what playerData.role was doing here
end
)

this would be how it should look like for the other way

addEventHandler("onPlayerJoin", root,
function()
--playerData = {}
     playerData.mode = 1;
outputChatBox('Welcome to our server', source, 0, 255, 0) -- player does not exist here, what you need is source, the source of the event in this case is the player you want, the wiki pages of the different events will always tell you what the source of the events will be
outputChatBox('Your current mode is: '..modeName(playerData.mode)..'.', source, 0, 255, 0) -- not quite sure what playerData.role was doing here
end
)

the only difference is the use of () instead of [] in line 6

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