Jump to content

Starting item with incomplete durability


Recommended Posts

Hello!

I wanted to ask your advice.
Is it possible to do so that the starting item has a durability of 50%?
This need for "winterhat".

local OnPlayerSpawn = function(src, player)
    player.prev_OnNewSpawn = player.OnNewSpawn
    player.OnNewSpawn = function()
   
        local start_items = {}
        
		--for spring
		if GLOBAL.TheWorld.state.isspring or (GLOBAL.TheWorld.state.iswinter and GLOBAL.TheWorld.state.remainingdaysinseason < 3) then
			table.insert(start_items, "grass_umbrella")
		end
		--for summer
		if GLOBAL.TheWorld.state.issummer or (GLOBAL.TheWorld.state.isspring and GLOBAL.TheWorld.state.remainingdaysinseason < 3) then
			table.insert(start_items, "watermelonhat")
		end
		--for winter
		if GLOBAL.TheWorld.state.iswinter or (GLOBAL.TheWorld.state.isautumn and GLOBAL.TheWorld.state.remainingdaysinseason < 3) then
			for i = 1, 3 do table.insert(start_items, "cutgrass") end
			for i = 1, 2 do table.insert(start_items, "log") end
			table.insert(start_items, "winterhat")
		end

        for k,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)

 

Edited by Tezumoto
Link to comment
Share on other sites

Doing a simple tweak will be enough

        for k,v in pairs(start_items) do
            local item = GLOBAL.SpawnPrefab(v)
            
            player.components.inventory:GiveItem(item)
        end   

To

        for k,v in pairs(start_items) do
            local item = GLOBAL.SpawnPrefab(v)
            if item.components.fueled ~= nil then
                item.components.fueled:SetPercent:(.5)
            end
            player.components.inventory:GiveItem(item)
        end   
Link to comment
Share on other sites

2 minutes ago, YakumoYukari said:

Doing a simple tweak will be enough


        for k,v in pairs(start_items) do
            local item = GLOBAL.SpawnPrefab(v)
            
            player.components.inventory:GiveItem(item)
        end   

To


        for k,v in pairs(start_items) do
            local item = GLOBAL.SpawnPrefab(v)
            if item.components.fueled ~= nil then
                item.components.fueled:SetPercent:(.5)
            end
            player.components.inventory:GiveItem(item)
        end   

This for all items?
I just need for one item)

Link to comment
Share on other sites

 if v:HasTag("beefalo") and item.components.fueled ~= nil then
   item.components.fueled:SetPercent:(.5)
end

I don't like inform something very specific case but if you want to tweak durability just one for the beefalo hat,
this will be enough.

Edited by YakumoYukari
Link to comment
Share on other sites

4 minutes ago, YakumoYukari said:

 if v:HasTag("beefalo") and item.components.fueled ~= nil then
   item.components.fueled:SetPercent:(.5)
end

I don't like inform something very specific case but if you want to tweak durability just one for the beefalo hat,
this will be enough.

Error
Need little fix for this:

        for k,v in pairs(start_items) do
            local item = GLOBAL.SpawnPrefab(v)
            if item.components.fueled ~= nil and item.prefab == "winterhat" then
                item.components.fueled:SetPercent(.5)
            end
            player.components.inventory:GiveItem(item)
        end   

item.components.fueled:SetPercent:(.5) -> item.components.fueled:SetPercent(.5)
":" this not need =)
Thx for help

Edited by Tezumoto
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...