Jump to content

Recommended Posts

So I'd like to make it so that a specific character of mine has a chance of getting a random item instead of what they intended to craft whenever they make an item. Unfortunately, I have absolutely no idea where to start for this particular process.

local loots = {...}

AddPrefabPostInit("xxxxx",function(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    inst:DoTaskInTime(0,function(inst)
        local owner = inst.components.inventoryitem.owner
        if owner ~= nil then
            inst.components.inventoryitem:RemoveFromOwner(true)
            local container = owner.components.inventory or owner.components.container or nil
            if container ~= nil then
                local item = loots[math.random(#loots)].item
                local loot = SpawnPrefab(item)
				if loot == nil then return end
                container:GiveItem(loot)
            end
        else
            local item = loots[math.random(#loots)].item
            local loot = SpawnPrefab(item)
			if loot == nil then return end
            loot.Transform:SetPosition(inst.Transform:GetWorldPosition())
          	
            if loot.Physics ~= nil then
                local angle = math.random() * 2 * PI
                loot.Physics:SetVel(2 * math.cos(angle), 10, 2 * math.sin(angle))
            end
        end
        inst:Remove()
    end)
end)

小心嵌套

Edited by _Yue_Yue_

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
×
  • Create New...