Jump to content

Recommended Posts

Hello!

I been trying to get a teleport function to work, though I seem to have hit a problem.

It keeps saying randomteleport is a nil value.

 

 

This is the part having trouble in the modmain.

if GetPlayer() and GetPlayer().prefab == "esctemplate" then     GetPlayer().randomteleport(GetPlayer())end

And this is the function in the character's lua.

local function randomteleport(inst)			local x2, y2, z2 = inst.Transform:GetWorldPosition()			local x, y, z = inst.Transform:GetWorldPosition()			x = x+math.random(-20, 20)			z = z+math.random(-20, 20)			inst.Transform:SetPosition(x,y,z)			  if not inst:IsOnValidGround() then			    inst.Transform:SetPosition(x2,y2,z2)			    randomteleport(inst)		          endend

Does anyone by any chance know what the problem is?

@HungryBerryBush Your function is a local function, which exists only in the character's file. It's not a child of the player's instance.

When are you calling that function? Maybe you can do it directly in the player's file.

 

 

If you must use the function outside of it, you'll need some reference to it.

 

Firstly, don't call GetPlayer multiple times, just store the value in a variable. It might not matter too much in this case, but it's generally not very good practice to call a function which will always return the same thing.

local player = GetPlayer()if player and player.prefab == "esctemplate" then     player.randomteleport(player)end

Note that you are sending in "player" to a function that is a child of "player". You can use the colon syntax too for simplicity.

-- "player" will be sent as the first parameter.player:randomteleport()-- The above is the same as this.-- player.randomteleport(player)

(It would be best to create the function the same way too, but we're getting off track)

 

 

And finally, to set a reference to the function in your player instance. (in your character's file)

-- Where inst is your player instanceinst.randomteleport = randomteleport

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
×
  • Create New...