HungryBerryBush Posted March 12, 2015 Share Posted March 12, 2015 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())endAnd 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) endendDoes anyone by any chance know what the problem is? Link to comment https://forums.kleientertainment.com/forums/topic/51976-calling-a-character-function-from-the-modmain/ Share on other sites More sharing options...
Blueberrys Posted March 12, 2015 Share Posted March 12, 2015 @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)endNote 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 https://forums.kleientertainment.com/forums/topic/51976-calling-a-character-function-from-the-modmain/#findComment-621131 Share on other sites More sharing options...
Mobbstar Posted March 12, 2015 Share Posted March 12, 2015 Why don't you set up that function in modmain? You can put the function in your characters fn though, like so:local function fn() [...] function inst:randomteleport() [...] end [...]end Link to comment https://forums.kleientertainment.com/forums/topic/51976-calling-a-character-function-from-the-modmain/#findComment-621132 Share on other sites More sharing options...
HungryBerryBush Posted March 13, 2015 Author Share Posted March 13, 2015 Thanks to the both of you!With this information I have been able to solve a secondary problem as well. Link to comment https://forums.kleientertainment.com/forums/topic/51976-calling-a-character-function-from-the-modmain/#findComment-621276 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now