Argonwolf Posted January 6, 2021 Share Posted January 6, 2021 I want to use the following code to get entities near each player: AddPlayerPostInit(function(inst) inst:DoPeriodicTask(1.0, function(inst) local pp = inst.Transform:GetWorldPosition() local ents = GLOBAL.TheSim:FindEntities(pp.x, pp.y, pp.z, 100) end) end) However it crashes my game with the following error: [string *../mods/MyMod/modmain.lua"]:25: attempt to index local 'pp' (a number value) LUA ERROR stack traceback: ../mods/MyMod/modmain.lua:25 in (field) fn (Lua) <23-26> scripts/scheduler.lua:177 in (method) OnTick (Lua) <155-207> scripts/scheduler.lua:371 in (global) RunScheduler (Lua) <369-377> scripts/update.lua:185 in () ? (Lua) <164-243> Any idea what I'm doing wrong? I'm brand new to modding DST so I don't know if I have a syntax error or what. Link to comment Share on other sites More sharing options...
penguin0616 Posted January 6, 2021 Share Posted January 6, 2021 (edited) Transform:GetWorldPosition() returns x, y, z, not a Vector. Edited January 6, 2021 by penguin0616 1 1 Link to comment Share on other sites More sharing options...
icuranme Posted January 6, 2021 Share Posted January 6, 2021 penguin is correct. I recommend trying local x, y, z = inst.Transform:GetWorldPosition() local ents = GLOBAL.TheSim:FindEntities(x, y, z, 100) I'm new to lua and moding, but I hope this helps. 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted January 6, 2021 Share Posted January 6, 2021 (edited) This being said, you can use GetPosition but it's a vector3: local pp = inst.Transform:GetPosition() print(pp.x, pp.y, pp.z) -- By part manually print(pp:Get()) -- Returns the x, y, and z components There are advantages in keeping it as a Vec3 as there are wrapper functions with the object type to make doing math with them simpler. They're all defined in scripts/vector3.lua. Edited January 6, 2021 by CarlZalph 1 Link to comment Share on other sites More sharing options...
Argonwolf Posted January 6, 2021 Author Share Posted January 6, 2021 It worked, thanks guys. Though I don't get why pp.x etc... don't work and you have to use pp:GET(), as surely if you can print them they're public members and can be accessed directly? At any rate that doesn't matter to the mod, which now works as intended. Thanks again! Link to comment 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