Forum

> > CS2D > Scripts > FUNCTION - Teleport a player to a random tile.
Forums overviewCS2D overview Scripts overviewLog in to reply

English FUNCTION - Teleport a player to a random tile.

8 replies
To the start Previous 1 Next To the start

old FUNCTION - Teleport a player to a random tile.

Mami Tomoe
User Off Offline

Quote
Hi I need a function that I give it the value ID and it will teleport the given ID to a random place on the map but the only tile he will spawn on be tile #1 and no other!

So:
Function + search for a random tile #1 location + teleport player.

Thanks

old Re: FUNCTION - Teleport a player to a random tile.

Waldin
User Off Offline

Quote
Here my code, yay.
Thanks to this, now i know more lua! yay! (everytime i code something, i learn more and more lol)
If you want teleport someone to tile 1 call it teleportRandomTile(ID,1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
local frames = {}
for frame = 0,map("tilecount") do
	frames[frame] = {}
end
for x = 0,map("xsize") do
	for y = 0,map("ysize") do
		table.insert(frames[tile(x,y,"frame")],{x,y})
	end
end
math.randomseed(os.time())
function teleportRandomTile(id,frame)
	if frames[frame] then
		if #frames[frame] > 0 then
			local x,y = unpack(frames[frame][math.random(#frames[frame])])
			parse("setpos "..id.." "..x*32+16 .." "..y*32+16)
		else
			error("no tiles with frame #"..frame,2)
		end
	else
		error("frame not found",2)
	end
end
edited 1×, last 17.08.16 04:02:14 am

old Re: FUNCTION - Teleport a player to a random tile.

Mami Tomoe
User Off Offline

Quote
Hi error
1
2
3
4
5
6
LUA ERROR: sys/lua/cs2dtibia/items.lua:245: frame not found
 -> [C]: in function 'error'
 -> sys/lua/cs2dtibia/items.lua:859: in function 'teleportRandomTile'
 -> sys/lua/cs2dtibia/items.lua:245: in function '?'
 -> sys/lua/cs2dtibia/hooks.lua:340: in function <sys/lua/cs2dtibia/hooks.lua:309>
 -> in Lua hook 'menu', params: 1, 1, 251, 0, 0, -99, 0

I want player to be teleported to a random tile but the tile should be grass which in my tile set the tile ID is 1 are you sure it's the right script?

old Re: FUNCTION - Teleport a player to a random tile.

Rainoth
Moderator Off Offline

Quote
I'm pretty sure frames are ordered by integers, not strings.
So basically there's
frame 1
frame 2
frame 3

but not
frame "1"
frame "2"
frame "3"

user Waldin's script tries to index the parameter you pass - "1"
so it's looking for frames["1"] when there's actually frames[1].

Just use
1
teleportRandomTile(id,1)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview