goatt Posted May 6, 2023 Share Posted May 6, 2023 I want to make a mod that gives new players extra extra starting items. But I don't want to give them to those who just want to switch characters through lunar portal. How can I accomplish that? Thanks! Link to comment https://forums.kleientertainment.com/forums/topic/147643-how-to-distinguish-newly-joined-from-switching-character-on-spawning-players/ Share on other sites More sharing options...
lakhnish Posted May 6, 2023 Share Posted May 6, 2023 Just of the top of my head, you can probably make a conditional that checks the player's age to give them your extra-extra items. Link to comment https://forums.kleientertainment.com/forums/topic/147643-how-to-distinguish-newly-joined-from-switching-character-on-spawning-players/#findComment-1633845 Share on other sites More sharing options...
goatt Posted May 6, 2023 Author Share Posted May 6, 2023 (edited) 6 hours ago, lakhnish said: Just of the top of my head, you can probably make a conditional that checks the player's age to give them your extra-extra items. Thanks for replying! So I checked the age, but it seems the age is always 0 after using the lunar portal. But when I used the same function right after the spawn, the age is normal (several days old). Code below Spoiler local OnPlayerSpawn = function(src, player) print(player.prev_OnNewSpawn) print(player.OnNewSpawn) player.prev_OnNewSpawn = player.OnNewSpawn player.OnNewSpawn = function() local start_items = { } print(player.components.age:GetAge()) if player.components.age:GetAge() <= 1 then -- new player get new player resources insert(start_items, "cutgrass", 40) else -- old players gets advanced stuff if count("greenstaff") < 1 then insert(start_items, "greenstaff", 1) end end -- add items to player for _,v in pairs(start_items) do local item = GLOBAL.SpawnPrefab(v) player.components.inventory:GiveItem(item) end if player.prev_OnNewSpawn ~= nil then player:prev_OnNewSpawn() player.prev_OnNewSpawn = nil end end end local function ListenForPlayers(inst) if GLOBAL.TheWorld.ismastersim then inst:ListenForEvent("ms_playerspawn", OnPlayerSpawn) end end AddPrefabPostInit("world", ListenForPlayers) Because I know very little about Lua or modding. what is .prev_OnNewSpawn? Are both .prev_OnNewSpawn and .OnNewSpawn instance methods of player? Edited May 6, 2023 by goatt Link to comment https://forums.kleientertainment.com/forums/topic/147643-how-to-distinguish-newly-joined-from-switching-character-on-spawning-players/#findComment-1633885 Share on other sites More sharing options...
lakhnish Posted May 6, 2023 Share Posted May 6, 2023 4 hours ago, goatt said: Because I know very little about Lua or modding. what is .prev_OnNewSpawn? Are both .prev_OnNewSpawn and .OnNewSpawn instance methods of player? Unfortunately, I've got no clue. As for it going back to zero, maybe try something with adding tags to players and checking for that tag, though I'm not sure if even that would work. Link to comment https://forums.kleientertainment.com/forums/topic/147643-how-to-distinguish-newly-joined-from-switching-character-on-spawning-players/#findComment-1633936 Share on other sites More sharing options...
goatt Posted May 7, 2023 Author Share Posted May 7, 2023 2 hours ago, lakhnish said: Unfortunately, I've got no clue. As for it going back to zero, maybe try something with adding tags to players and checking for that tag, though I'm not sure if even that would work. Idk much about tags or anything. But thank you for your help! Link to comment https://forums.kleientertainment.com/forums/topic/147643-how-to-distinguish-newly-joined-from-switching-character-on-spawning-players/#findComment-1633947 Share on other sites More sharing options...
CarlZalph Posted May 8, 2023 Share Posted May 8, 2023 On 5/6/2023 at 4:13 AM, goatt said: I want to make a mod that gives new players extra extra starting items. But I don't want to give them to those who just want to switch characters through lunar portal. How can I accomplish that? Thanks! Look at the data/scripts/prefabs/player_common.lua file's OnNewSpawn function. It uses the following check: if TheWorld.components.playerspawner ~= nil and TheWorld.components.playerspawner:IsPlayersInitialSpawn(inst) then Followed up by sending an event: TheWorld:PushEvent("ms_newplayerspawned", inst) You should be able to listen for this event and do the same check as above in your callback to do what you want. You can self-validate the call flow by looking at what calls these, and end up in data/scripts/networking.lua where the function SpawnNewPlayerOnServerFromSim calls player:OnNewSpawn before playerspawner:SpawnAtNextLocation (which marks IsPlayersInitialSpawn to be false). 1 Link to comment https://forums.kleientertainment.com/forums/topic/147643-how-to-distinguish-newly-joined-from-switching-character-on-spawning-players/#findComment-1634078 Share on other sites More sharing options...
goatt Posted May 9, 2023 Author Share Posted May 9, 2023 Thank you so much! I'll look it up! @CarlZalph Link to comment https://forums.kleientertainment.com/forums/topic/147643-how-to-distinguish-newly-joined-from-switching-character-on-spawning-players/#findComment-1634302 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