RandomTP
#1
Hi, its my first plugin in LUA.
I don't know how to check the ID of block in random XYZ position.
Sometimes its move me into blocks. I want to teleport olny on grass, sand, dirt.
Please help Smile
PHP Code:
function Randomtp(SplitPlayer)
    if (
#Split ~= 1) then
        
Player:SendMessage("Usage: /rtp")
        return 
true
    
else
        
math.random(-20002000)
        
math.random(40,70)
        
=  math.random(-20002000)
        
Player:TeleportToCoords(XYZ)
        
Player:SendMessage(cChatColor.Yellow .. "Cords")
        
Player:SendMessage("X: " .. X)
        
Player:SendMessage("Y: " .. Y)
        
Player:SendMessage("Z: " .. Z)
    
end
    
return true
end 
Reply
Thanks given by:
#2
Hello, and welcome to the forum Smile

You can use this:
local BlockID = Player:GetWorld():GetBlock(X, Y, Z)
if ((BlockID == E_BLOCK_GRASS) or (BlockID == E_BLOCK_DIRT) or (BLOCKID == E_BLOCK_SAND)) then
   -- Teleport the player
end

But keep in mind that a player can now be teleported inside block. If you use this:
local X = math.random(-2000, 2000)
local Z = math.random(-2000, 2000)
local Succes, Y = Player:GetWorld():TryGetHeight(X, Z)

the player should teleport to the highest block instead.
Reply
Thanks given by:
#3
I used :
PHP Code:
        X math.random(-20002000)
    
=  math.random(-20002000)
    
Player:GetWorld():GetHeight(XZ

Trying to use TryGetHeight(X, Z), returned bool value and error in colsole.
Reply
Thanks given by:
#4
TryGetHeight() returns two different variables on completion. The first is a boolean which indicates if the chunk is even loaded and a second which is the highest block point on those X and Z coordinates. You would have to set TryGetHeight() equal to 2 variables and use the second one to get the right value.

local IsChunkLoaded
local Height

IsChunkLoaded, Height = Player:GetWorld():TryGetHeight(X, Z)

Reply
Thanks given by:
#5
[Image: err.png]

PHP Code:
function Initialize(Plugin)
    
Plugin:SetName("RandomTP")
    
Plugin:SetVersion(1)

    
cPluginManager.BindCommand("/rtp""randomTP.rtp"Randomtp" ~ Random Teleport");

    
LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
    return 
true
end

function Randomtp(SplitPlayer)
    if (
#Split ~= 1) then
        
Player:SendMessage("Usage: /rtp")
        return 
true
    
else
        
math.random(-20002000)
        
=  math.random(-20002000)
        
local IsChunkLoaded
        local Height
        IsChunkLoaded
Height Player:GetWorld():TryGetHeight(XZ)
        
Player:TeleportToCoords(XHeightZ)
        
Player:SendMessage(cChatColor.Yellow .. "Cords")
        
Player:SendMessage("X: " .. X)
        
Player:SendMessage("Y: " .. Y)
        
Player:SendMessage("Z: " .. Z)
    
end
    
return true
end 
Reply
Thanks given by:
#6
The chunk is most likely not loaded. Try this:
local X = math.random(-2000, 2000)
local Z = math.random(-2000, 2000)
local _, Height

local World = Player:GetWorld()
World:ChunkStay({math.floor(X / 16), math.floor(Z / 16)}, nil, 
	function()
		_, Height = World:TryGetHeight(X, Z)
	end
)

It will ensure that the chunks are loaded and then get the height.


Btw, I didn't test this code. There might be a syntax error in it.
Reply
Thanks given by:
#7
NiLSPACE,
[Image: errr.png]

It works with GetHeight(X, Z). But sometimes it is teleporting me before chunk load, and im locking in blocks
Reply
Thanks given by:
#8
Ooh, I see the problem. Try:
World:ChunkStay({{math.floor(X / 16), math.floor(Z / 16)}}, nil, 
Reply
Thanks given by:
#9
PHP Code:
argument #3 is nil ; 'number' expected 
Reply
Thanks given by:
#10
That's weird. Could you post the whole code + whole error?
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)