Jump to content

Making hats unequippable for character


Recommended Posts

I'm porting Waxworth to DST, and it's going okay. One of the problems I have though is that I can't get the code that stops the character being able to equip hats to work.

 

I'm an amateur at modding DS, but I pretty much know why it's not working - because it's trying to assign a code to a character prefab that doesn't exist yet, as the world is created before you choose your character in DST. But I'm not sure what I should be doing to get it not crash on starting a server.

 

I know this code technically works, because I tested on an already created test world with the character, but I cannot create a new world with this code in place, so I need to find another way to do it.

 

This is the section of code from modmain.lua:

 

 


local function ModHat(inst)    if GLOBAL.GetPlayer().prefab == "waxworth" then    inst:RemoveComponent("equippable")endendAddPrefabPostInit('strawhat', ModHat)AddPrefabPostInit('tophat', ModHat)AddPrefabPostInit('beefalohat', ModHat)AddPrefabPostInit('featherhat', ModHat)AddPrefabPostInit('beehat', ModHat)AddPrefabPostInit('minerhat', ModHat)AddPrefabPostInit('spiderhat', ModHat)AddPrefabPostInit('footballhat', ModHat)AddPrefabPostInit('earmuffshat', ModHat)AddPrefabPostInit('winterhat', ModHat)AddPrefabPostInit('bushhat', ModHat)AddPrefabPostInit('flowerhat', ModHat)AddPrefabPostInit('walrushat', ModHat)AddPrefabPostInit('slurtlehat', ModHat)AddPrefabPostInit('ruinshat', ModHat) local function ModBushHat(inst)    if GLOBAL.GetPlayer().prefab == "wax" then    inst:RemoveComponent("equippable")        inst:RemoveComponent("useableitem")endendAddPrefabPostInit('bushhat', ModBushHat)

 

Any help or guidance with this would be much appreciated.

 

aAfK95R.png

 

Link to comment
Share on other sites

local function ModHat(inst)     if GLOBAL.GetPlayer().prefab == "waxworth" then     inst:RemoveComponent("equippable") end end
 Okay, this is not the right way to do it at all. Firstly, GetPlayer() is a trap, which is why it prints out a warning in the log whenever you use it in DST. This is because there are now multiple players, so you need to be more careful about how you acquire a given player and make sure you get the player that's actually relevant to the situation. If you DO want the local player object, you should get it with ThePlayer instead (it's a variable, not a function). Secondly, by removing the equippable component means nobody will be able to equip it. The behavior of that block of code right now is that if the host is Waxworth, nobody will be able to equip hats at all.

 

The reason you can't make a new world with this code is because if the world contains a straw hat or miner hat from world generation, it will be executing the GetPlayer() parts before you have a character at all, so GetPlayer() will return nil, resulting in attempt to index nil, and crash.

 

Now, as for an actual solution, there was a similar case posted a few weeks ago where someone wanted a character that couldn't equip armor. I'd recommend trying the approach detailed there.

Link to comment
Share on other sites

I assume you have Thunder Volt's permission to port it, yes?

 

Yes.

 

 Okay, this is not the right way to do it at all. Firstly, GetPlayer() is a trap, which is why it prints out a warning in the log whenever you use it in DST. This is because there are now multiple players, so you need to be more careful about how you acquire a given player and make sure you get the player that's actually relevant to the situation. If you DO want the local player object, you should get it with ThePlayer instead (it's a variable, not a function). Secondly, by removing the equippable component means nobody will be able to equip it. The behavior of that block of code right now is that if the host is Waxworth, nobody will be able to equip hats at all.

 

The reason you can't make a new world with this code is because if the world contains a straw hat or miner hat from world generation, it will be executing the GetPlayer() parts before you have a character at all, so GetPlayer() will return nil, resulting in attempt to index nil, and crash.

 

Now, as for an actual solution, there was a similar case posted a few weeks ago where someone wanted a character that couldn't equip armor. I'd recommend trying the approach detailed there.

 

 

Okay, thank you for the information. I will check out the linked treat and try the approach there. I appreciate the reply!

Link to comment
Share on other sites

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
 Share

×
  • Create New...