TUTORIAL - Creating your first server + savePos script

Need advices or help troubleshooting? Ask for help here.
Post Reply
Karm
Posts: 3
Joined: Fri Oct 12, 2018 12:30 pm

TUTORIAL - Creating your first server + savePos script

Post by Karm » Tue Oct 16, 2018 9:42 pm

Hi!
I have decided to do this tutorial for newbies and for those who like programming and who want to experiment with this great game. In this tutorial we will do:
- Download the server.
- Write our first script.
- Test it at game.

First of all, you need to download the server going to the Downloads section in this forum viewforum.php?f=11&sid=277ea1b63b06d3890d1606176cfabc62:
Image

Here, click in Downloads:
Image

And we select the last version of the server. In this case, Beta 1.0 - Patch 5.
Image

After download it, we create a folder for store the server. I create the folder C:/IV-MP Server
Image

Unzip the content of the server at folder.
Image

For run the server, we execute IVMP Lua.exe
Image

But first, we are going to create our first script. At filterscripts folder, we create a file called savePos.lua, a script for save our position at the game typing /savepos at chat.
Image

We will write this code at file. This script read the commands and if we type /savepos save the spawn position at positions.txt and shows a message for the player. You have more info at script comments.:

Code: Select all

-- http://lua-users.org/wiki/CommonFunctions
-- remove trailing and leading whitespace from string.
-- http://en.wikipedia.org/wiki/Trim_(programming)
function trim(s)
  -- from PiL2 20.4
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function commandProcessor(playerid, text)
	-- Remove whitespaces from input text
	local command = trim(text)
	if(command == "/savepos") then
		-- Gets player position
		local x, y, z = getPlayerPos(playerid)
	
		-- https://www.tutorialspoint.com/lua/lua_file_io.htm
		-- Opens a file in append mode
		file = io.open("positions.txt", "a")

		-- sets the default output file as positions.txt
		io.output(file)

		-- appends the player command info to the last line of the file
		io.write("setPlayerPos("..x..", "..y..", "..z..")\n")

		-- closes the open file
		io.close(file)
		
		sendPlayerMsg(playerid, "Position saved!: x: "..x..", y: "..y..", z: "..z..")", 0xFFFF00FF)
	end	
end
registerEvent("commandProcessor", "onPlayerCommand")
And in main.lua (at server main folder), we will add the line. All script we want to use must be loaded here:

Code: Select all

loadScript("savePos")
Image

OK, let's try the script! Run your server (close first if it was open) and connect with your client.
Image

When you be in, press T for write at chat and type /savepos:
Image

If all was fine, we will read the position info:
Image

And we will have the info at positions.txt:
Image

You can use this command for store spawn positions.

That's all. Do you need some help at any point?
If you liked the tutorial and you want more tell me at comments.
See you soon!!

SamyrCastello
Posts: 17
Joined: Sun Jan 21, 2018 6:55 am

Re: TUTORIAL - Creating your first server + savePos script

Post by SamyrCastello » Fri Oct 26, 2018 3:18 am

i cant enter in my server Connection Attempt Failed

Karm
Posts: 3
Joined: Fri Oct 12, 2018 12:30 pm

Re: TUTORIAL - Creating your first server + savePos script

Post by Karm » Sat Oct 27, 2018 11:50 am

Hi SamyrCastello,
can you post a image capture of the error?

SamyrCastello
Posts: 17
Joined: Sun Jan 21, 2018 6:55 am

Re: TUTORIAL - Creating your first server + savePos script

Post by SamyrCastello » Sat Oct 27, 2018 10:12 pm

Server in Masterlist Image

SamyrCastello
Posts: 17
Joined: Sun Jan 21, 2018 6:55 am

Re: TUTORIAL - Creating your first server + savePos script

Post by SamyrCastello » Sat Oct 27, 2018 10:18 pm

see i cant connect on my server

Image

Suge
Site Admin
Posts: 61
Joined: Sat Jul 29, 2017 9:04 pm

Re: TUTORIAL - Creating your first server + savePos script

Post by Suge » Mon Oct 29, 2018 12:50 am

It's impossible to connect to a local host using IP, click on "Direct" instead and select your IPV4.

SamyrCastello
Posts: 17
Joined: Sun Jan 21, 2018 6:55 am

Re: TUTORIAL - Creating your first server + savePos script

Post by SamyrCastello » Mon Oct 29, 2018 1:50 pm

problem solved I changed the port in config.cfg and the router now i can connect thanks for the attention and help :D

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests