Jump to content

Calling a character function from the modmain?


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?

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...